469,934 Members | 2,607 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

I see no difference in an inheirited non-virtual method and an inheirited virtual method

I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_ABase : public ABase
{
public:
};

class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}
Jul 22 '05 #1
9 1691
jlopes wrote:
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_ABase : public ABase
{
public:
};
One generally has no reason to inherit unless one overrides a virtual class.
class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()
'void main' will make your 'nads drop off. Use 'int main'.
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Suppose Child overrode filter() too. Now suppose you have this function:

int foo(Base & b)
{
b.filter();
}

Now you can pass any Base object into foo(), or any Child object, or any
other object that inherits Base. The behavior of filter() will change. This
allows callers to customize foo()'s behavior without changing its source.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #2

"jlopes" <jl*******@COMCAST.NET> wrote in message
news:f1**************************@posting.google.c om...
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_ABase : public ABase
{
public:
};

class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Even if you do not override the inherited method, you can still see
diferences if the following conditions are met:

- some class E_ derives from class D_of_ABase and E_ overrides filter()
- you use references or pointers to class D_of_ABase
- you do not initialize the reference or pointer based of objects you
create,
but based of objects you receive from some other module or library
- the other module or library knows or defines class E_ and actualy
provides you
with references/pointers to objects of class E_ (this would be
perfectly legal C++)

then when you would call filter() through your reference/pointer to
C_of_ABase, you
will have the surprise of getting yourself a call to some other override of
filter() then the one in ABase (namely the one form E_).

Should you have used classes Base and Child in my example, you would have no
such surprise.

I think you can still explicitly call (with no surprises) the inherited
version of filter() like this:

D_of_ABase &Dobj = InvokeDObjectFactory() //Get reference to D from
outside
Dobj.ABase::filter() // Explicitly call the base class
override, no matter
// what was provided with the
reference form outside

"Timothy Madden"
Romania
Jul 22 '05 #3

"jlopes" <jl*******@COMCAST.NET> wrote in message
news:f1**************************@posting.google.c om...
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_ABase : public ABase
{
public:
};

class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Even if you do not override the inherited method, you can still see
diferences if the following conditions are met:

- some class E_ derives from class D_of_ABase and E_ overrides filter()
- you use references or pointers to class D_of_ABase
- you do not initialize the reference or pointer based of objects you
create,
but based of objects you receive from some other module or library
- the other module or library knows or defines class E_ and actualy
provides you
with references/pointers to objects of class E_ (this would be
perfectly legal C++)

then when you would call filter() through your reference/pointer to
C_of_ABase, you
will have the surprise of getting yourself a call to some other override of
filter() then the one in ABase (namely the one form E_).

Should you have used classes Base and Child in my example, you would have no
such surprise.

I think you can still explicitly call (with no surprises) the inherited
version of filter() like this:

D_of_ABase &Dobj = InvokeDObjectFactory() //Get reference to D from
outside
Dobj.ABase::filter() // Explicitly call the base class
override, no matter
// what was provided with the
reference form outside

"Timothy Madden"
Romania

Jul 22 '05 #4

"jlopes" <jl*******@COMCAST.NET> wrote in message
news:f1**************************@posting.google.c om...
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_ABase : public ABase
{
public:
};

class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Even if you do not override the inherited method, you can still see
diferences if the following conditions are met:

- some class E_ derives from class D_of_ABase and E_ overrides filter()
- you use references or pointers to class D_of_ABase
- you do not initialize the reference or pointer based of objects you
create,
but based of objects you receive from some other module or library
- the other module or library knows or defines class E_ and actualy
provides you
with references/pointers to objects of class E_ (this would be
perfectly legal C++)

then when you would call filter() through your reference/pointer to
C_of_ABase, you
will have the surprise of getting yourself a call to some other override of
filter() then the one in ABase (namely the one form E_).

Should you have used classes Base and Child in my example, you would have no
such surprise.

I think you can still explicitly call (with no surprises) the inherited
version of filter() like this:

D_of_ABase &Dobj = InvokeDObjectFactory() //Get reference to D from
outside
Dobj.ABase::filter() // Explicitly call the base class
override, no matter
// what was provided with the
reference form outside

"Timothy Madden"
Romania

Jul 22 '05 #5

"jlopes" <jl*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ } When having a virtual function you sould alway include a virtual destructor:
virtual ~ABase() {} };

class D_of_ABase : public ABase
{
public:
};

class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main() should be int main() {
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


There is no difference here as you do not override the virtual function, but
try doing that (print some statement in each function) and see what happens
here:

int main()
{
Base *base = new Child();
ABase *abase = new D_of_ABase();
base->filter();
abase->filter();
}

/Peter
Jul 22 '05 #6

"Phlip" <ph*******@yahoo.com> wrote in message
news:u3*******************@newssvr17.news.prodigy. com...

One generally has no reason to inherit unless one overrides a virtual class.


He means to say, "unless one overrides a virtual function." If you don't
override the function, then there is no point to it.
Jul 22 '05 #7
"Phlip" <ph*******@yahoo.com> wrote in message news:<u3*******************@newssvr17.news.prodigy .com>...
jlopes wrote:
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }
};

class D_of_ABase : public ABase
{
public:
};


One generally has no reason to inherit unless one overrides a virtual class.
class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()


'void main' will make your 'nads drop off. Use 'int main'.
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


Suppose Child overrode filter() too. Now suppose you have this function:

int foo(Base & b)
{
b.filter();
}

Now you can pass any Base object into foo(), or any Child object, or any
other object that inherits Base. The behavior of filter() will change. This
allows callers to customize foo()'s behavior without changing its source.


Thanks Phil

Your thought the in general there is no need to inherit is an answer
closer to what I'm looking for. If one has a basic set of behavior for
a given set of objects, and the individual objects has distinct
behavior, they would inherit behavior from the common object
(Polymorphisim). As for the "void main" heh just wanted the code to
compile, sorry the code was not politically correct. As for the foo()
method heh polymorphism I'm aware of.

My quesstion was centered round to virtual or not to virtual.
Jul 22 '05 #8
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message news:<kB********************@news000.worldonline.d k>...
"jlopes" <jl*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
I'm looking at the differences between these to forms and see no
difference in their use. When accessed through a derived class.

class ABase
{
public:
virtual void filter(){ /* some code */ }

When having a virtual function you sould alway include a virtual destructor:
virtual ~ABase() {}
};

class D_of_ABase : public ABase
{
public:
};

class Base
{
public:
void filter(){ /* some code */ }
};

class Child : public Base
{
publc:
};

void main()

should be int main()
{
Child* child = new Child();
D_of_ABase* doab = new D_of_ABase();

child->filter();
doab->filter();
}


There is no difference here as you do not override the virtual function, but
try doing that (print some statement in each function) and see what happens
here:

int main()
{
Base *base = new Child();
ABase *abase = new D_of_ABase();
base->filter();
abase->filter();
}

/Peter


Thanks Peter, I thought there wasn't a difference.

I deliberately left out the details to gen a debate on this subject.
The d_tor was not part of the subject. I left out the override to
limit the generating a discussion of polymorphism (it didn't work
but...heh).

In trying to print something do you suspect the linker would mixup the
class/method signature? Hmm that woud be ugly, intresting.
Jul 22 '05 #9

"jlopes" <jl*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:<kB********************@news000.worldonline.d k>...
"jlopes" <jl*******@COMCAST.NET> skrev i en meddelelse
news:f1**************************@posting.google.c om...
> I'm looking at the differences between these to forms and see no
> difference in their use. When accessed through a derived class.
>
> class ABase
> {
> public:
> virtual void filter(){ /* some code */ }

When having a virtual function you sould alway include a virtual
destructor:
virtual ~ABase() {}
> };
>
> class D_of_ABase : public ABase
> {
> public:
> };
>
> class Base
> {
> public:
> void filter(){ /* some code */ }
> };
>
> class Child : public Base
> {
> publc:
> };
>
> void main()

should be int main()
> {
> Child* child = new Child();
> D_of_ABase* doab = new D_of_ABase();
>
> child->filter();
> doab->filter();
> }


There is no difference here as you do not override the virtual function,
but
try doing that (print some statement in each function) and see what
happens
here:

int main()
{
Base *base = new Child();
ABase *abase = new D_of_ABase();
base->filter();
abase->filter();
}

/Peter


Thanks Peter, I thought there wasn't a difference.

I deliberately left out the details to gen a debate on this subject.
The d_tor was not part of the subject. I left out the override to
limit the generating a discussion of polymorphism (it didn't work
but...heh).

In trying to print something do you suspect the linker would mixup the
class/method signature? Hmm that woud be ugly, intresting.


Ugly? Not at all - this is intended behaviour:
base->filter() will call the filter in base as base::filter is not virtual.
abase->filter() will call the filter in D_of_ABase. This is because
ABase::filter is virtual so the filter of the actual object, which here is
of type D_of_ABase, will be called.

/Peter
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Raj Dhrolia | last post: by
11 posts views Thread by Shea Martin | last post: by
7 posts views Thread by Alex Vinokur | last post: by
12 posts views Thread by Petronius | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.