Archived

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

Destroyer

Inhearitence question...

Recommended Posts

Destroyer    122
Hi, I''m trying to implement this neat little class hiearchy (dunno if I spelled that right ) and I''m having a little dillema. I have a base class called CSurface.
  

class CSurface
{
 public:
 //member functions go here

 protected:
 UCHAR* surface;
 private:
 int xpos;
 int ypos;
}

  
Of course I''m leaving out a lot of crap, and its not exactly what I have, I''m just making this up to explain my problem. Now, having that base class I want to create a class off of that called CVectSurface which inhearites from CSurface. Here''s the problem. I want it so I don''t have int xpos or int ypos but instead of them I want int* ypos and int* xpos. I don''t want those private members inhearited! I repeat - I DON''T want those private members inhearited. Yet when I did inhearate from the base class and I made an variable (or instance?) of that class when the list of members showed up on MSVC++ it showed all the members from the base class, including the ones under private. Any help ? Thanks, Destroyer

Share this post


Link to post
Share on other sites
Shannon Barber    1681
The reason they show-up is because they take up space in CVectSurface objects - if CVectSurface inherits from CSurface, then CVectSurface is a CSurface and thus requires everything that makes up a CSurface.

    
ISurface
{
//pure virtual methods that *ALL* surfaces will have

};

class CSurface : public ISurface
{
public:
//override ISurface methods

private:
int m_xpos;
int m_ypos;
};

class CVectSurface : public ISurface
{
public:
//override ISurface methods

private:
int* m_xpos;
int* m_ypos;
}



That will let you have completely different implementations for ISurface, but let you treat them all the same. A nominal performance cost is paid for this ability, and it could be significant at this level of granuality (every surface).

Magmai Kai Holmlor

"Oh, like you've never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Edited by - Magmai Kai Holmlor on December 9, 2001 3:12:14 PM

Share this post


Link to post
Share on other sites
Desert Fox    277
Yes, it shows up in the Visual C++ member list, but it is not really inherited, it''s just a problem with the VC++ IDE. If you declare int* xpos; in the derived class, it will show up as an int*, not as an int.


Desert Fox

Share this post


Link to post
Share on other sites
Shannon Barber    1681
I''m afriad it really is inherited... the ClassView is easily confused, don''t take it''s word on anything.

It''s just hidden by your definition in the derived class, you need to use scope::resolution operators or a base class pointer to access the int forms on xpos and ypos.

If you declare a int* xpos in the derived class it will have 2 properties named xpos, one an int, the other an int*.

  
class CSurface
{
public:
int xpos, ypos;
};

class CVectSurface : private CSurface
{
public:
int* xpos;
int* ypos;
};

#include <iostream.h>
int main(int argc, char* argv[])
{
cout<<"Sizeof surf: "<<sizeof(CSurface)<<endl;
cout<<"Sizeof vect: "<<sizeof(CVectSurface)<<endl;

return 0;
}

CSurface is 8 bytes, CVectSurface is 16.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Desert Fox    277
Well, that''s interesting, I stand corrected . I was always under the impression that private members are not inherited to derived classes. If you try to access the derived private member in the derived class, you will get a compiler error however. Other than the size, are there any other issues involved with this? ie, Are there any potential conflicts in the derived class when declaring members private in the base class?

Desert Fox

Share this post


Link to post
Share on other sites
invective    118
quote:

I was always under the impression that private members are not inherited to derived classes.



Private members ARE inherited, they are just not visible. This is necessary, as public functions in the base class interface are inherited in the derived class, and the public base class functions may need to access private base class members.

  
class base {
int x;
public:
void set_x ( int i ) {
x = i;
}
int get_x ( void ) {
return x;
}
}

class derived public:base {
base (int i) {
//This is ok

set_x(i);

//But this will not compile

x = i;
}
}

void main (void) {
derived child (1);
//this is ok

child.set_x(5);
//this is ok too

std::cout << child.get_x();

//but this fails to compile

child.x = 5;
}

Share this post


Link to post
Share on other sites