Derived and Base class variables 
Author Message
 Derived and Base class variables

Can you have a variable in  a derived class with the same name as in
the base class? I.e.

class Base
{
        int myint;

Quote:
}

class Derived : public Base
{
        int myint;

Quote:
}

I tried it and expected the compiler to complain but it didn't. Is the
compiler treating the second myint as merely a redeclaration of the
first?


Tue, 06 May 2003 08:53:02 GMT  
 Derived and Base class variables

Quote:
> Can you have a variable in  a derived class with the same name as in
> the base class? I.e.

> class Base
> {
> int myint;
> }

> class Derived : public Base
> {
> int myint;
> }

Yes

Quote:
> I tried it and expected the compiler to complain but it didn't. Is the
> compiler treating the second myint as merely a redeclaration of the
> first?

No.

You are declaring a new member that has the name myint

The member of the same name in the base class is still there, but you just
cannot directly access it from the derived class by name only

However you can access, for example, by prefixing the name with a class
scope
eg

void Derived::foo() {
    myint = 1;    // refers to myint in Derived
    Base::myint = 2;    // refers to myint in Base

Quote:
}

Its pretty much the same as function overloading .. the base class function
still exists, but when you use the function name (without scope etc) you get
the derived class version.  Of course, with functions, you also have the
issue of virtual functions.  There is no such thing as a virtual data member
though.

The means that

Derived* pDerived;
Base* pBase;
Derived x;
pDerived = &x;
pBase = &x;
pDerived->myint = 1; // refers to myint in Derived
pBase->myint = 1; // refers to myint in Base (even though pBase points to an
object of type Derived)
--
Roger Onslow
Software Developer
See my articles at http://www.codeguru.com
See the product I am working on at http://www.swishzone.com

"Operator .. give me the number for 911" .. Homer Simpson



Tue, 06 May 2003 08:59:14 GMT  
 Derived and Base class variables
Duh. Scope. Forgot about that. Thanks.

On Fri, 17 Nov 2000 11:59:14 +1100, "Roger Onslow"

Quote:

>> Can you have a variable in  a derived class with the same name as in
>> the base class? I.e.

>> class Base
>> {
>> int myint;
>> }

>> class Derived : public Base
>> {
>> int myint;
>> }

>Yes

>> I tried it and expected the compiler to complain but it didn't. Is the
>> compiler treating the second myint as merely a redeclaration of the
>> first?

>No.

>You are declaring a new member that has the name myint

>The member of the same name in the base class is still there, but you just
>cannot directly access it from the derived class by name only

>However you can access, for example, by prefixing the name with a class
>scope
>eg

>void Derived::foo() {
>    myint = 1;    // refers to myint in Derived
>    Base::myint = 2;    // refers to myint in Base
>}

>Its pretty much the same as function overloading .. the base class function
>still exists, but when you use the function name (without scope etc) you get
>the derived class version.  Of course, with functions, you also have the
>issue of virtual functions.  There is no such thing as a virtual data member
>though.

>The means that

>Derived* pDerived;
>Base* pBase;
>Derived x;
>pDerived = &x;
>pBase = &x;
>pDerived->myint = 1; // refers to myint in Derived
>pBase->myint = 1; // refers to myint in Base (even though pBase points to an
>object of type Derived)
>--
>Roger Onslow
>Software Developer
>See my articles at http://www.codeguru.com
>See the product I am working on at http://www.swishzone.com

>"Operator .. give me the number for 911" .. Homer Simpson



Tue, 06 May 2003 09:32:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to Create CCtrlView derived class based on CTreeCtrl derived class

2. How to Create CCtrlView derived class based on CTreeCtrl derived class

3. How to Stop Derived Class from overriding base class method

4. creating an instance of a derived class from a base class

5. How to prevent derived classes from changing a base class function

6. address of base class and derived class different

7. Deriving from CControlBar - no base class in New Class dialog

8. CDialog derived class as a base class for another dialog box

9. serializing a class derived from an abstract base class

10. Serialize class derived from virtual base class

11. How do I cast base class to a derived class

12. Base ptrs & Derived Classes;Please HELP

 

 
Powered by phpBB® Forum Software