do static functions get inherited?

A

ashok

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
 
V

Victor Bazarov

ashok said:
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
 
V

Victor Bazarov

DaKoadMunky said:
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
 
D

DaKoadMunky

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.
 
A

ARatio

DaKoadMunky said:
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
 
D

DaKoadMunky

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;
 
D

DaKoadMunky

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.
 
D

David White

DaKoadMunky said:
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
 
D

DaKoadMunky

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.
 
V

Victor Bazarov

DaKoadMunky said:
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
 
D

David Lindauer

David said:
DaKoadMunky said:
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
 
V

Victor Bazarov

David Lindauer said:
[...]
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
 
D

David White

David Lindauer said:
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
 
M

Marcelo Pinto

Victor Bazarov said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top