Solved

Changing member data in a derived class

Posted on 2004-03-28
13
218 Views
Last Modified: 2010-04-01
If I have a virtual function in a derived class how do use it to change a member variable of the base class.  I'm using an example that I've used recently on this site.  Its basically a hierachy of computers, with the PC class being the base and Compaq's, Dells etc being the derived ones.  I have function that needs to perform an update of the PC components.  The components are basically base class pointers to seperate pc compoents.  Therefore the overloaded upgraded function accepts a pointer for a new component as a paremeter and then needs to change the existing component pointer to that of the new component.  when I try and access the pointer which is a private member variable of the base PC class I use a function that returns the pointer I need to update.  Do I have to create another base member function called (swapPointer()) or something just to update the member variable or is their something else I can do.....

Please help

Janusz
0
Comment
Question by:jstabik
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 10698506
Difficult to say without seeing code; please post yours !
0
 
LVL 7

Expert Comment

by:leflon
ID: 10698634
hi janusz,

i am not sure i understand what you do with the pointer and the funcs, but if your upgrade function is in the derived class, you can not access the private data members. you have to use a base class function to do this.

but i agree with mrwad99, some code would be really nice.

leflon
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 10698747
It may be smarter to make the data members in the base class protected rather than private.  That way any class that's derived from the base class (PC) can access and modify these data members, but any class or function that isn't derived from PC can't modify the data members.  That's what I usually do if it's an option.  Hope this helps.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:jstabik
ID: 10698753
Heres a go at supplying the code, though I think I may have sorted the problem out but have come accross a stumbling block.

Base Class
----------------------------------------

class cPC
{
public:
      cPC(cCPU*, cCDRom*, cHardDrive*, cMemory*, cMotherBoard*, cVideoCard*);
      virtual ~cPC();
      void identify();
      cCPU * upgrade(cCPU*);
      cMemory * upgrade(cMemory*);
      cCDRom * upgrade(cCDRom*);
      cHardDrive * upgrade(cHardDrive*);
      cMotherBoard * upgrade(cMotherBoard*);
      cVideoCard * upgrade(cVideoCard*);
      bool validateChip();
      virtual cMemory* testF();
      
private:
      cCPU *pCPU;
      cCDRom *pCDRom;
      cHardDrive *pHDrive;
      cMemory *pMemory;
      cMotherBoard *pMother;
      cVideoCard *pVideo;
};

Base Class CPP
-------------------------------------------------
cMemory* cPC::testF()
{
      cMemory * omem = new cMemory(500,1,2,"Crucial","SpeedMem");
      return omem;
}

Derived Class
------------------------------------------------
class cPcCompaqPresario : public cPcCompaq
{
      public:
            cPcCompaqPresario(cCPU*, cCDRom*, cHardDrive*, cMemory*, cMotherBoard*, cVideoCard*);
            cMemory* testF();
            
      private:
            
};

Derived Class CPP
-------------------------------------------------
cMemory* cPcCompaqPresario::testF()
{
      cMemory * Mem2 = new cMemory(500,1,2,"Crucial","SpeedMem");
      cout <<"testf" << endl;
      return Mem2;
}



I've cut out all of the irrelevant code.  The problem now being that I'm getting a link error with the virtual function - testF.  I stopped working with the upgrade function I mentioned earlier as I thought it may just be a typo, so I created the testF function and removed the functionality.  If the function returns a data type such as an int or string there is no error.  But in changing the return type to a pointer I get the following error:

pcCompaq.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall cPC::testF(void)" (?testF@cPC@@UAEXXZ)
Debug/Inheritance.exe : fatal error LNK1120: 1 unresolved external

Whu am I getting this error????

HELP!!!!
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 10699061
This is because you are not defining the body for testF() in all of the base classes.

Post the code in main() that uses the above then we can give a clearer explanation.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10699072
It looks like the return type for the version of testF is void, but the testF that you're showing us has a return type of cMemory*.  There's probably a typo somewhere there.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10699340
You have a a virtual prototype of function testF in your base class, but the implementation is missing.

You may do this in in your base class header:

        virtual cMemory* testF() { return NULL; }

Then, you have an implementation and it should compile.

Regards, Alex


0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10699350
itsmeandnobodyelse, jstabik posted this code above:
cMemory* cPC::testF()
{
     cMemory * omem = new cMemory(500,1,2,"Crucial","SpeedMem");
     return omem;
}

This is the implementation of testF().  The issue is that somewhere he's declaring it as return void, but it's actually returning cMemory *.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 10947222
I think that mnashadka has given the solution....
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10947380
I think, that mnashadka is right, so he should get the points.

The link error states "virtual void cPC::testF is missing". Most likely the questioner changed the return type somewhen and there is still an object file or precompiled header referring to the old definition.

Regards, Alex
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question