Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > do static functions get inherited?

Reply
Thread Tools

do static functions get inherited?

 
 
ashok
Guest
Posts: n/a
 
      11-09-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2004
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
 
Reply With Quote
 
 
 
 
DaKoadMunky
Guest
Posts: n/a
 
      11-09-2004
>Any member function is inherited

What about assignment operators?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2004
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:perator=(d2);
}

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

V
 
Reply With Quote
 
DaKoadMunky
Guest
Posts: n/a
 
      11-10-2004
>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:perator=();
};


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

I don't know which one is correct.
 
Reply With Quote
 
ARatio
Guest
Posts: n/a
 
      11-10-2004

"DaKoadMunky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >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: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


 
Reply With Quote
 
DaKoadMunky
Guest
Posts: n/a
 
      11-10-2004
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;
 
Reply With Quote
 
DaKoadMunky
Guest
Posts: n/a
 
      11-10-2004
>>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.
 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      11-10-2004
"DaKoadMunky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >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:perator= is not inherited. From a Derived member you can
explicitly call Base: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



 
Reply With Quote
 
DaKoadMunky
Guest
Posts: n/a
 
      11-10-2004
>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static functions and C++ member functions mafiQ C++ 0 04-22-2009 08:33 PM
Static inline functions with static local variables Martin Wells C Programming 10 10-08-2007 03:38 PM
lvalue difference between static and non static member functions tkrogc@gmail.com C++ 9 02-06-2006 02:06 PM
static and non-static functions Victor Bazarov C++ 4 04-02-2005 04:15 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments