Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Confused about non-virtual functions
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 19th August 06:08
victor bazarov
External User
 
Posts: 1
Default Confused about non-virtual functions


You've been writing C++ for "about 8 years" and still cannot
understand one simple thing: it's NOT the same object. Once
you manage to comprehend this, you'll understand why calling
a function with the same name _should_ be different _unless_
it's a virtual function.

To help you understand what it means "not the same object", please run this:
#include <iostream>

struct A {
double foo;
};

struct B {
int a;
void func() {
std::cout << (void*)this << std::endl;
}
};

struct D : A, B {
void func() {
std::cout << (void*)this << std::endl;
}
};

int main() {
D d;
D *pd = &d;
B *pb = &d;
void *p = pd;
std::cout << "As D : " << p << std::endl;
p = pb;
std::cout << "As B : " << p << std::endl;
pd->func();
pb->func();
}

Does it output the same address? If it doesn't, how can
"the same object" have different addresses?

Think about it before you reply next time.

Victor
  Reply With Quote


  sponsored links


2 19th August 06:08
david white
External User
 
Posts: 1
Default Confused about non-virtual functions


My argument was in the context of the example that _I_ gave. Why give a
completely different one?

class Base
{
public:
void func();
//...
};

class Derived : public Base
{
public:
void func();
//...
};

void f()
{
Derived d;
Base *pBase = &d;
Derived *pDerived = &d;
pBase->func();
pDerived->func();
}

Aside from the pointers, how many objects have been created here? One, a
Derived. That's all.

Nevertheless, I'll look at yours.


Only one object here, a D.


Points to the D part of d, which of course includes the A and B parts, which it inherits.


Points to the B part of d, which might or might not have the same address as
the D part. In any case, we are still pointing to a D, because that's the
type that d is.


Converts the address of d to void*, with who-knows what result.


Because you are pointing to different parts of the _same_ object. So what?

David
  Reply With Quote
3 19th August 06:08
david white
External User
 
Posts: 1
Default Confused about non-virtual functions


There is ONE object. One D. That's what you created.

Wrong. There is only a D.

No, they are not. Suppose you have a class Shape, which is abstract, and
from it you derive a class Ellipse. If you have a Shape* that points to an
Ellipse, you are not pointing to a Shape object, because there is no such
thing. You are pointing to the Shape part of an Ellipse. After all, that's
what you made, an Ellipse. Further, I suggest that on most implementations
the Shape* and Ellipse* would be the same address. Does that mean, according
to your own reasoning, that they point to the same object?


Yes, you are. d is a D, so you are pointing to a D.

I'm not going to repeat something that's wrong. I suggest you repeat this:
"pb points to the same object"

Yes, it still points to d, which is a D. To prove that it does, add a
virtual function to A that is overridden in D. Whether you call the function
through an A* or a D* the result is the same: it calls the function in D.
That's because the object you are pointing to is a D.

Being a sarcastic arsehole generally doesn't facilitate constructive discussions.


I've expressed an _opinion_ as to the usage of identical, non-virtual
functions in base and derived classes, and I've given my reasons.

Instinctively. And I was referring only to the usage we are discussing.
Where did "I know that it's full of it, all wrong" come from? Not from me,
even though you've presented this crap as a quote from me.

I said that I had not seen the usage we are discussing in 8 years of
programming C++. I neither stated nor implied anything else. I even
qualified what I've come across in that time with "Maybe I'm too sheltered,
but that's my experience." Perhaps you can explain how you managed to turn
that into the bullshit above.


I won't disagree with that, but it doesn't include the nonsense you've been
saying in your last couple of posts.

I've said nothing about the "ultimate truth". I've been expressing an
opinion on a specific usage.


My instinct can say what it likes. I am entitled to dislike a usage that is
easily missed because it's rare and contradicts the concept of IS-A in OO
programming.

David
  Reply With Quote
4 19th August 06:11
alexander terekhov
External User
 
Posts: 1
Default Confused about non-virtual functions


So next time try to not waste your time and common bandwidth
"discussing" something with Bazarov.

"Objects can contain other objects, called sub-objects. A sub-object
can be a member sub-object (9.2), a base class sub-object (clause 10),
or an array element. An object that is not a sub-object of any other
object is called a complete object."

regards,
alexander.
  Reply With Quote
5 19th August 06:12
dittoc
External User
 
Posts: 1
Default Confused about non-virtual functions


operator=

Here are some references why this should not be made virtual.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=slrna9k9r5.jdl.elflord%40panix2.panix.com&rnum=10
http://groups.google.com/groups?q=virtual+%22operator%3D%22+problems&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=3A2E0CA1.56CC5759%40dresdner-bank.com&rnum=7
http://groups.google.com/groups?q=virtual+%22operator%3D%22+problems&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=8ggq8o%24dak%241%40nnrp1.deja.com&rnum=2

Dave
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666