tags:

views:

160

answers:

3

I have this class which has a double list template of a struct of two chars and another struct

typedef struct KeyC{
     char K[5];
     char C[9];
} TKeyC;

typedef struct Bin{
     char Car;
     char Cad[9];
     TKeyC *KC;
} TBin;

class Bo {
    private:
     TDoubleList<TBin> *Ent;

    public:
            ...
}

I have a method to create nodes. No problem with this, but when I call other method to modify the direction of the pointer to a new TKeyC struct this simply doesn't happen.

TNode<TBin> *Aux;
TKeyC *AuxKC=new TKeyC;
Aux->getObj().KC=AuxKC;

Do I have to use a class instead of a struct in this case or is a problem of structs or there is a bug in it?

Update

template <class T>
class TNode
{
    private:
      T TObj;
      TNode<T> *Prev,*Next;

    public:
     TNode();
     ~TNode();
     TNode(T);
     void setObj(T);
     void sPrev(TNode<T>*);
     void sNext(TNode<T>*);
     T getObj();
     TNode<T>* gPrev();
            TNode<T>* gNext();
};

And the method getObj:

template <class T>
T TNode<T>::getObj() {return(TObj);};
+6  A: 

A structure in C++ is a class whose members are all public. If your assignment isn't changing the value you want it to, then you are not assigning to what you think you are.

Your getObj() function returns a copy of the structure, not a reference to the original. So you update the value in the copy, and the original remains unchanged.

Get a reference to your object, either by changing the return type of getObj() to T& or adding another function if you have other code that depends on the behavior.

David Crawshaw
Darryl
A: 

Why you use the keyword typedef when you are declaring an struct ?

msantamaria
Old 'C' coding habits die hard, I would guess.
Steve Gilham
Yes, I think so
msantamaria
That's right, can't control my habits. This would be better? struct { ... } TKeyC;
Nek
No, it should be just `struct TKeyC { ... };`
Pavel Minaev
A: 

Does this work for you?

TBin x = Aux->getObj();
x.KC=AuxKC;

If getObj() is returning a pointer to the object, use x->KC=AuxKC

Ashwin