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);};
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()toT&or adding another function if you have other code that depends on the behavior.