do static functions get inherited?

Discussion in 'C++' started by ashok, Nov 9, 2004.

  1. ashok

    ashok Guest

    I have a confusion.
    Do static member functions of base class get inherited to derived
    class?

    I searched for this answer but nowhere I get any referencec saying
    "derived class inherits static functions with other member functions
    of the base class".


    regds,
    arj
    ashok, Nov 9, 2004
    #1
    1. Advertising

  2. ashok wrote:
    > I have a confusion.
    > Do static member functions of base class get inherited to derived
    > class?


    Yes.

    > I searched for this answer but nowhere I get any referencec saying
    > "derived class inherits static functions with other member functions
    > of the base class".


    Any member function is inherited (unless it's private). It may be
    inaccessible due to name hiding, but it is always there.

    Are you experiencing any particular problem?

    V
    Victor Bazarov, Nov 9, 2004
    #2
    1. Advertising

  3. ashok

    DaKoadMunky Guest

    >Any member function is inherited

    What about assignment operators?
    DaKoadMunky, Nov 9, 2004
    #3
  4. DaKoadMunky wrote:
    >>Any member function is inherited

    >
    >
    > What about assignment operators?


    Yes. The problem is that they are hidden by class' own assignment
    operator (even if it's compiler-defined).

    #include <iostream>
    struct B {
    B& operator=(const B&) {
    std::cout << "B=B\n"; return *this; }
    };

    struct D : B {};

    int main() {
    D d1, d2;
    d1.B::eek:perator=(d2);
    }

    Constructors and destructors are not inherited. But those are
    truly special functions.

    V
    Victor Bazarov, Nov 9, 2004
    #4
  5. ashok

    DaKoadMunky Guest

    >Yes. The problem is that they are hidden by class' own assignment
    >operator (even if it's compiler-defined).
    >
    > #include <iostream>
    > struct B {
    > B& operator=(const B&) {
    > std::cout << "B=B\n"; return *this; }
    > };
    >
    > struct D : B {};
    >
    > int main() {
    > D d1, d2;
    >


    I am hoping that Santa Claus will bring me the C++ Standard for Christmas, but
    until then I will have to quote TCPPPL 2nd Ed. pg 593...

    "The assignment function operator=() must be a nonstatic member function; it is
    not inherited"

    Maybe this is an error or has changed?

    struct Base
    {
    void operator=(int)
    {
    }
    };

    struct Derived : Base
    {
    };

    int main()
    {
    Derived d;
    d = 3;

    return 0;
    }

    The above produces an error on both Comeau and VC++.

    If I introduce a using declaration into struct Derived...

    struct Derived : Base
    {
    using Base::eek:perator=();
    };


    Comeau accepts d = 3 but VC++ still does not.

    I don't know which one is correct.
    DaKoadMunky, Nov 10, 2004
    #5
  6. ashok

    ARatio Guest

    "DaKoadMunky" <> wrote in message
    news:...
    > >Yes. The problem is that they are hidden by class' own assignment
    > >operator (even if it's compiler-defined).
    > >
    > > #include <iostream>
    > > struct B {
    > > B& operator=(const B&) {
    > > std::cout << "B=B\n"; return *this; }
    > > };
    > >
    > > struct D : B {};
    > >
    > > int main() {
    > > D d1, d2;
    > >

    >
    > I am hoping that Santa Claus will bring me the C++ Standard for Christmas,

    but
    > until then I will have to quote TCPPPL 2nd Ed. pg 593...
    >
    > "The assignment function operator=() must be a nonstatic member function;

    it is
    > not inherited"
    >
    > Maybe this is an error or has changed?
    >
    > struct Base
    > {
    > void operator=(int)
    > {
    > }
    > };
    >
    > struct Derived : Base
    > {
    > };
    >
    > int main()
    > {
    > Derived d;
    > d = 3;
    >
    > return 0;
    > }
    >
    > The above produces an error on both Comeau and VC++.
    >
    > If I introduce a using declaration into struct Derived...
    >
    > struct Derived : Base
    > {
    > using Base::eek:perator=();
    > };
    >
    >
    > Comeau accepts d = 3 but VC++ still does not.
    >
    > I don't know which one is correct.



    Question about this topic:

    Why should I overload the operator= instead of create a copy constructor?



    ernesto
    ARatio, Nov 10, 2004
    #6
  7. ashok

    DaKoadMunky Guest

    Copy constructor is used to construct an object that is a copy of an existing
    object...

    T t1;
    T t2(t1);

    Copy assignment is used to assign an existing object to an existing object...

    T t3,t4;
    t3 = t4;
    DaKoadMunky, Nov 10, 2004
    #7
  8. ashok

    DaKoadMunky Guest

    >>Why should I overload the operator= >>instead of create a copy constructor?

    >
    >Copy constructor is used to construct an object that is a copy of an existing
    >object...
    >
    >T t1;
    >T t2(t1);
    >
    >Copy assignment is used to assign an existing object to an existing object...
    >
    >T t3,t4;
    >t3 = t4;


    I wanted to clarify which post I was responding to.
    DaKoadMunky, Nov 10, 2004
    #8
  9. ashok

    David White Guest

    "DaKoadMunky" <> wrote in message
    news:...
    > >Yes. The problem is that they are hidden by class' own assignment
    > >operator (even if it's compiler-defined).
    > >
    > > #include <iostream>
    > > struct B {
    > > B& operator=(const B&) {
    > > std::cout << "B=B\n"; return *this; }
    > > };
    > >
    > > struct D : B {};
    > >
    > > int main() {
    > > D d1, d2;
    > >

    >
    > I am hoping that Santa Claus will bring me the C++ Standard for Christmas,

    but
    > until then I will have to quote TCPPPL 2nd Ed. pg 593...
    >
    > "The assignment function operator=() must be a nonstatic member function;

    it is
    > not inherited"


    The 3rd edition says: "Note that if you don't define a copy assignment
    operator, the compiler will generate one. This implies that assignment
    operators are not inherited." Doesn't make sense to me (see below).

    > Maybe this is an error or has changed?
    >
    > struct Base
    > {
    > void operator=(int)
    > {
    > }
    > };
    >
    > struct Derived : Base
    > {


    Because you haven't defined your own operator= here, a default is provided
    for you, which will hide the one in the base class. But that doesn't mean
    that Base::eek:perator= is not inherited. From a Derived member you can
    explicitly call Base::eek:perator=(int) if you like. To me, that makes it
    inherited, in the same way that any ordinary member whose name happens to be
    hidden is still inherited.

    > };
    >
    > int main()
    > {
    > Derived d;
    > d = 3;
    >
    > return 0;
    > }


    [snip]

    DW
    David White, Nov 10, 2004
    #9
  10. ashok

    DaKoadMunky Guest

    >Because you haven't defined your own operator= here, a default is provided
    >for you, which will hide the one in the base class.


    Victor opened the door and you kicked it open.

    I wasn't looking at it from the name hiding perspective for some reason.

    Still, what does it really mean that it is not inherited? As you point
    out...how is this any different from name hiding with a ordinary member?

    Anyone know what the effect of the using declaration should have been? As
    pointed out I got different results with two compilers.
    DaKoadMunky, Nov 10, 2004
    #10
  11. "DaKoadMunky" <> wrote...
    > >Because you haven't defined your own operator= here, a default is
    > >provided
    >>for you, which will hide the one in the base class.

    >
    > Victor opened the door and you kicked it open.
    >
    > I wasn't looking at it from the name hiding perspective for some reason.
    >
    > Still, what does it really mean that it is not inherited? As you point
    > out...how is this any different from name hiding with a ordinary member?
    >
    > Anyone know what the effect of the using declaration should have been? As
    > pointed out I got different results with two compilers.


    Well, I didn't respond to your post about VC++ and Comeau, but my money
    is on Comeau all the time AFA Standard compliance is concerned.

    The Standard in clause 10, paragraph 1 says "Unless redefined in
    the derived class, members of a base class are also considered to be
    members of the derived class. The base class members are said to be
    inherited by the derived class. Inher-ited members can be referred to
    in expressions in the same manner as other members of the derived
    class, unless their names are hidden or ambiguous (10.2)." For any
    name that is hidden you can fully qualify it (using :: operator).
    The call will still be _valid_.

    Constructors and destructors are not inherited because there is no
    way for them to be called -- they don't have names.

    V
    Victor Bazarov, Nov 10, 2004
    #11
  12. David White wrote:

    > "DaKoadMunky" <> wrote in message
    > news:...
    > > >Yes. The problem is that they are hidden by class' own assignment
    > > >operator (even if it's compiler-defined).
    > > >
    > > > #include <iostream>
    > > > struct B {
    > > > B& operator=(const B&) {
    > > > std::cout << "B=B\n"; return *this; }
    > > > };
    > > >
    > > > struct D : B {};
    > > >
    > > > int main() {
    > > > D d1, d2;
    > > >

    > >
    > > I am hoping that Santa Claus will bring me the C++ Standard for Christmas,

    > but
    > > until then I will have to quote TCPPPL 2nd Ed. pg 593...
    > >
    > > "The assignment function operator=() must be a nonstatic member function;

    > it is
    > > not inherited"

    >
    > The 3rd edition says: "Note that if you don't define a copy assignment
    > operator, the compiler will generate one. This implies that assignment
    > operators are not inherited." Doesn't make sense to me (see below).
    >
    > > Maybe this is an error or has changed?
    > >
    > > struct Base
    > > {
    > > void operator=(int)
    > > {
    > > }
    > > };
    > >
    > > struct Derived : Base
    > > {

    >
    > Because you haven't defined your own operator= here, a default is provided
    > for you, which will hide the one in the base class. But that doesn't mean
    > that Base::eek:perator= is not inherited. From a Derived member you can
    > explicitly call Base::eek:perator=(int) if you like. To me, that makes it
    > inherited, in the same way that any ordinary member whose name happens to be
    > hidden is still inherited.
    >
    > > };
    > >
    > > int main()
    > > {
    > > Derived d;
    > > d = 3;
    > >
    > > return 0;
    > > }

    >
    > [snip]
    >
    > DW


    what do the compiler generator assignment operators do?

    Thanks,

    David
    David Lindauer, Nov 10, 2004
    #12
  13. "David Lindauer" <> wrote...
    > [...]
    > what do the compiler generator assignment operators do?


    It invokes member-wise copying. Certain members get copied regardless
    the absence of the assignment op for them (arrays) and certain members
    cannot be copied (references) although assignment is defined for them.

    V
    Victor Bazarov, Nov 10, 2004
    #13
  14. ashok

    David White Guest

    "David Lindauer" <> wrote in message
    news:...
    >
    > what do the compiler generator assignment operators do?


    They do a member-by-copy of all data members in the class, after first
    calling any base class assignment operator(s) taking the default argument
    type (whether compiler or user defined).

    DW
    David White, Nov 10, 2004
    #14
  15. Victor Bazarov <> wrote in message news:<Okakd.8401$09.us.to.verio.net>...
    > ashok wrote:
    > > I have a confusion.
    > > Do static member functions of base class get inherited to derived
    > > class?

    >
    > Yes.
    >
    > > I searched for this answer but nowhere I get any referencec saying
    > > "derived class inherits static functions with other member functions
    > > of the base class".

    >
    > Any member function is inherited (unless it's private). It may be
    > inaccessible due to name hiding, but it is always there.
    >
    > Are you experiencing any particular problem?
    >
    > V


    I believed private members were inherited, but inaccessible. I believe
    they are there but you can't call them directly. Could you clarify
    this point?

    Marcelo Pinto
    Marcelo Pinto, Nov 10, 2004
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov

    static and non-static functions

    Victor Bazarov, Apr 2, 2005, in forum: C++
    Replies:
    4
    Views:
    7,148
    Stephen Howe
    Apr 2, 2005
  2. Replies:
    9
    Views:
    931
    Ben Bacarisse
    Feb 6, 2006
  3. mangesh
    Replies:
    4
    Views:
    394
    Jonathan Mcdougall
    Jun 4, 2006
  4. wes
    Replies:
    1
    Views:
    302
  5. 7stud --
    Replies:
    11
    Views:
    386
    7stud --
    Nov 9, 2007
Loading...

Share This Page