Solved

Changing member data in a derived class

Posted on 2004-03-28
13
216 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now