Board index » cppbuilder » classes & inheritance...?
Robert G. Hoover
![]() CBuilder Developer |
Robert G. Hoover
![]() CBuilder Developer |
classes & inheritance...?2005-04-20 10:23:30 PM cppbuilder100 Hello, This is a pretty basic question... :) I've got a base class and an inheriting class... class BaseClass { public: void DoSomething(); private: int BaseVar; }; class MyBaseClass : public BaseClass { }; ...so, when I call MyBaseClass::DoSomething() as it is inherited, BaseClass::BaseVar is undefined. Is there a way to fix this easily with "inherited" or "virtual" or the like? Thanks so much!!! Rob |
maeder
![]() CBuilder Developer |
2005-04-20 11:29:34 PM
Re:classes & inheritance...?
"Robert G. Hoover" < XXXX@XXXXX.COM >writes:
Quoteclass BaseClass QuoteIs there a way to fix this easily with "inherited" or "virtual" or |
Ed Mulroy [TeamB]
![]() CBuilder Developer |
2005-04-20 11:43:25 PM
Re:classes & inheritance...?
The visiblities of both a function and a variable is controlled in the
same way by the public, protected and private cases. The keyword virtual only applies to functions. It does not alter the effect of public, protected and private. class BaseClass { public: void DoSomething(); private: int BaseVar; }; class MyBaseClass : public BaseClass { }; class BaseClass { public: void DoSomething(); // visible to all protected: void DoSomethingElse(); // visible to descendent classes private: int BaseVar; // visible only to BaseClass }; class MyBaseClass : public BaseClass { int int_var; MyBaseClass() { DoSomething(); // ok DoSomethingElse(); // ok int_var = BaseVar; // no good, not visible } }; int main() { MyBaseClass mbc; mbc.DoSomething(); // ok; mbc.DoSomethingElse(); // no good, not visible int x = mbc.BaseVar; // no good, not visible return 0; } . Ed QuoteRobert G. Hoover wrote in message {smallsort} |
Liz Albin
![]() CBuilder Developer |
2005-04-21 12:09:03 AM
Re:classes & inheritance...?
On Wed, 20 Apr 2005 10:23:30 -0400, Robert G. Hoover wrote:
QuoteBaseClass::BaseVar is undefined. Is there a way to fix this easily with Good luck, liz |
Robert G. Hoover
![]() CBuilder Developer |
2005-04-21 12:12:39 AM
Re:classes & inheritance...?
When I cursor over MyVar in the de{*word*81}, I see MyVar=????
What I've done here is to create MyBaseClass when Application->Run(). I am not creating BaseClass. So, I have MyBaseClass running and I think when I'm calling DoSomething(), it's referencing the MyVar in the BaseClass which is uninitialized/undefined instead of (inherited and initialized) MyVar in MyBaseClass. Does this make more sense? Thanks again! Rob ____________________________________________________________________________ "Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message Quote"Robert G. Hoover" < XXXX@XXXXX.COM >writes: |
maeder
![]() CBuilder Developer |
2005-04-21 05:02:23 AM
Re:classes & inheritance...?
"Robert G. Hoover" < XXXX@XXXXX.COM >writes:
QuoteWhen I cursor over MyVar in the de{*word*81}, I see MyVar=???? QuoteWhat I've done here is to create MyBaseClass when Application->Run(). I am before the program even runs. QuoteSo, I have MyBaseClass running and I think when I'm calling QuoteDoes this make more sense? not too much) code so that others can see what you are seeing? |