Archived

This topic is now archived and is closed to further replies.

Bleakcabal

Problem with overloaded operators, templates and inheritance

Recommended Posts

Hi ! I have a base class and a derived class ( which inherits the base class ). I want to derived class to overload the [] operator, so that it can perform certain operations and also call the [] operator of the base class. I have used to BaseClass before and I know it isn''t the source of the problem. I have abreviated some code with ( ... ) when I felt it was not necessary to put said code. Here is what I have done so far : Code : DerivedClass.h template DerivedClass : public BaseClass { public : ... ... ... T &operator[] (int param); }; DerivedClass.cpp ... ... ... template T &DerivedClass::operator[] (int param) { ... ... ... } Main.cpp ... DerivedClass test(); ... test = 12; ... But I get a Segmentation Fault everytime I run the program. I have removed all code in the overloaded operator [] in the derived class so I know this is not the problem. If I call test[i] = 12 I get a segfault even if [] does not have any code in it''s implementation. If I don''t call [] I don''t have any problems. I have tried different values, etc. but after some extensive testing I can see the problem is not there. I have no idea what I could be doing wrong... but I really have close to no experience overloading operators and working with templates so I''m no surprised it''s not working. I would appreciate any help you could give me, thank you in advance !

Share this post


Link to post
Share on other sites
A few thoughts: One, you''re putting the implementation of a function in a template class in a separate source file. This may lead to bad mojo.

Two, you''re calling the derived class''s constructor with the default arguments which may or may not be initializing the base class''s constructor so that it has storage. This may be the case if you''re deriving from a container class like std::vector.

Three: learn to use the [source][/source] tag. Templated code does not show up happily outside it without work.

Share this post


Link to post
Share on other sites
(1) You should probably make your operator virtual in (at least) the base class. Otherwise, if you have a pointer whose type is BaseClass*, but it''s actually pointing to a DerivedClass, it will call BaseClass::operator[] against the instance of DerivedClass, when what you (almost definitely) want is for it to call DerivedClass::operator[], which can then call BaseClass::operator[] if it sees fit.

(2) If your operator[] (or any function, for that matter) doesn''t have a return statement, but is expected to return something (i.e. isn''t void), what it will actually do when you call it is return some garbage data. So, if you''re doing something like T myT = myBaseClass[17], myT will get filled up with this garbage data, which is probably where your seg fault is coming from.

(3) This one''s a little more iffy, but as I understand it (and, correct me if you know I''m wrong, gamedev.net-reading public!) when you declare a pramater or a return type with the & symbol, it''s a lot like having a pointer. If you returned a pointer to a variable that was declared inside of your function, that data is no longer valid as soon as the function returns. So, anything you do to that data will probably have unexpected results, since other functions are free to write to that spot in memory once the function returning the pointer ended.

Um.. maybe an example would be a little more clear?


int globalVariable = 7;

int* returnGoodPointer()
{
// return a pointer to a global variable -- this is OK.
return &globalVariable;
}

int* returnBadPointer()
{
int localVariable = 13;

// return a pointer to a variable in this function -- this is bad, because
// localVariable will be clobbered as soon as returnBadPointer() returns.
return &localVariable;
}

int& returnGoodReference()
{
// return a reference to a global variable -- this is also OK.
return globalVariable;
}

int& returnBadReference()
{
int localVariable = 13;

// return a reference to a variable in this function -- this might be bad.
return localVariable;
}


So... I guess the short version is, when you have a function (or operator) whose return type has that little &, make sure you''re returning something that won''t go out of scope as soon as the function returning it is finished.

Share this post


Link to post
Share on other sites