473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Override virtual member functions in Subclasses

Hi,

I have a problem using member functions in derived classes that
override virtual member functions of base classes.

The following pieces of (simplified) code don't work. Can anybody give
me some hints on what might be wrong?

// BEGIN OF SAMPLE CODE
class Strategy
{
public:
Strategy() {}
virtual void run() { cout<<"Please override!"; }
};

class StrategyA: public Strategy
{
public:
StrategyA() {}
void run();
};
void StategyA::run()
{
cout <<"Functionality A";
}
class SomeClass
{
public:
SomeClass(Strategy s);

void doSomething();

private:
Strategy s_;
};

SomeClass::SomeClass(Strategy s): s_(s) {}

SomeClass::doSomething()
{
s_.run();
}
int main()
{
StategyA s;
SomeClass c(s);

c.doSomething();

return 0;
}
// END OF SAMPLE CODE
The above program will print out "Please override!" instead of
"Functionality A". Scanning through Stroustrup I can't seem to find
what is wrong.

An additional remark: I know it is better to declare the function
"run" in Strategy to be pure virtual (virtual void run() = 0;) but
that won't compile. The compiler gives error in SomeClass' constructor
and private member declaration. What do I forget?

Thanks In Advance,

With Kind Regards

Ruben Van Havermaet.
Jul 22 '05 #1
22 2908
"Ruben Van Havermaet" <rv******@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...
Hi,

I have a problem using member functions in derived classes that
override virtual member functions of base classes.

The following pieces of (simplified) code don't work. Can anybody give
me some hints on what might be wrong?

// BEGIN OF SAMPLE CODE
You know this, but I'm going to go ahead and put it here:

#include <iostream>
using std::cout;
class Strategy
{
public:
Strategy() {}
virtual void run() { cout<<"Please override!"; }
};

class StrategyA: public Strategy
{
public:
StrategyA() {}
void run();
Style: the run() function above is virtual; it's best to go ahead and say
"virtual" to remind you what it is.
};
void StategyA::run()
void StrategyA::run()
{
cout <<"Functionality A";
}
class SomeClass
{
public:
SomeClass(Strategy s);
Style: constructors and destructors are inline no matter what you do, so
it's usually best just to put the constructor/destructor definitions inside
the class like this:

SomeClass(Strategy s) : s_(s) { }

Also, initializing a data member like this has no effect. The private
member variable Strategy s_ is already a Strategy. "Initializing" it with a
StrategyA has no effect. There is no data that needs to be initialized, and
you can't initialize a object with another object anyway.

Change your constructor to:

SomeClass() { }

void doSomething();

private:
Strategy s_;
SomeClass' data member is a Strategy, not a StrategyA. If you changed this
to

StrategyA s_;

Then it will work.
};

SomeClass::SomeClass(Strategy s): s_(s) {}
Erase the line above.

SomeClass::doSomething()
void SomeClass::doSomething()

If you don't supply a return type, int is assumed. Since this function
returns no value, void is required.
{
s_.run();
}
int main()
{
StategyA s;
This should be

StrategyA s;

except that it shouldn't be there.
SomeClass c(s);
Change this to

SomeClass c;

After all these changes, the program should work.

c.doSomething();
This will print "Functionality A" now.

return 0;
}
// END OF SAMPLE CODE
The above program will print out "Please override!" instead of
"Functionality A". Scanning through Stroustrup I can't seem to find
what is wrong.

An additional remark: I know it is better to declare the function
"run" in Strategy to be pure virtual (virtual void run() = 0;) but
that won't compile. The compiler gives error in SomeClass' constructor
and private member declaration. What do I forget?


Declaring a pure virtual function makes the class an ADT (abstract data
type). Because you cannot create an object of an ADT, it must have a
default constructor (but that's already there). The problem comes from
trying to give SomeClass a Strategy member. When you do this, a Strategy
object is "constructed" inside SomeClass, which is an error. The SomeClass
constructor error is basically the same; it comes from trying to initialize
an object of the Strategy ADT.

Here's my version of your code. It should have everything working.

#include <iostream>

using std::cout;

class Strategy

{

public:

Strategy() {}

virtual void run() { cout<<"Please override!"; }

};

class StrategyA: public Strategy

{

public:

StrategyA() {}

virtual void run();

};

void StrategyA::run()

{

cout <<"Functionality A\n";

}

class SomeClass

{

public:

SomeClass() { }

void doSomething();

private:

StrategyA s_;

};

void SomeClass::doSomething()

{

s_.run();

}

int main()

{

SomeClass c;

c.doSomething();

return 0;

}

//good luck!

//mike tyndall
Jul 22 '05 #2
Stephen Tyndall posted:
If you don't supply a return type, int is assumed. Since this function
returns no value, void is required.

Incorrect.

There are three and only three instances in which you may not specify a
return type (and in all three instances it is compulsory that you don't):

a) Constructors

b) Destructor

c) Type conversion operators
class Blah
{
public:

Blah();

~Blah();

operator int();
};
-JKop
Jul 22 '05 #3
Stephen Tyndall wrote:
"Ruben Van Havermaet" <rv******@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...
class SomeClass
{
public:
SomeClass(Strategy s);

Style: constructors and destructors are inline no matter what you do, so


Where did you get this nonsense?
it's usually best just to put the constructor/destructor definitions inside
the class like this:

SomeClass(Strategy s) : s_(s) { }

Also, initializing a data member like this has no effect.
What are you talking about?
The private
member variable Strategy s_ is already a Strategy. "Initializing" it with a
StrategyA has no effect.
Are you out of your mind?
There is no data that needs to be initialized, and
What if I later add data to be initialised?
you can't initialize a object with another object anyway.
What????

Change your constructor to:
[...]


I removed the rest of your nonsense. Please read up on polymorphism
before attempting to advise unsuspecting newbies.

V
Jul 22 '05 #4
Ruben Van Havermaet wrote:
I have a problem using member functions in derived classes that
override virtual member functions of base classes.

The following pieces of (simplified) code don't work. Can anybody give
me some hints on what might be wrong?
What is wrong is that you are trying to achieve polymorphic behaviour
without using pointers or references. That's rather impossible.

// BEGIN OF SAMPLE CODE
class Strategy
{
public:
Strategy() {}
virtual void run() { cout<<"Please override!"; }
};

class StrategyA: public Strategy
{
public:
StrategyA() {}
void run();
};
void StategyA::run()
{
cout <<"Functionality A";
}
class SomeClass
{
public:
SomeClass(Strategy s);
Change this to

SomeClass(Strategy& rs);

void doSomething();

private:
Strategy s_;
Change this to

Strategy& s_;
};

SomeClass::SomeClass(Strategy s): s_(s) {}
And this to

SomeClass::SomeClass(Strategy& s): s_(s) {}

SomeClass::doSomething()
{
s_.run();
}
int main()
{
StategyA s;
SomeClass c(s);

c.doSomething();

return 0;
}
// END OF SAMPLE CODE


Now it should work as expected.

Victor
Jul 22 '05 #5
"JKop" <NU**@NULL.NULL> wrote in message
news:mC******************@news.indigo.ie...
Stephen Tyndall posted:
If you don't supply a return type, int is assumed. Since this function
returns no value, void is required.


Incorrect.

There are three and only three instances in which you may not specify a
return type (and in all three instances it is compulsory that you don't):

a) Constructors

b) Destructor

c) Type conversion operators
class Blah
{
public:

Blah();

~Blah();

operator int();
};


This came from VC++.NET; when I moved his code into it and tried to compile,
it complained that the "function (name) differs from (name) only by return
type." He had declared it as void, and the output window said that the
function definition had an int return type. Another VC++ slipup?

//mike tyndall
Jul 22 '05 #6
On Tue, 03 Aug 2004 12:34:19 -0700, Stephen Tyndall wrote:
"JKop" <NU**@NULL.NULL> wrote in message
news:mC******************@news.indigo.ie...
Stephen Tyndall posted:
> If you don't supply a return type, int is assumed. Since this
> function returns no value, void is required.


Incorrect.

There are three and only three instances in which you may not specify a
return type (and in all three instances it is compulsory that you
don't):

a) Constructors

b) Destructor

c) Type conversion operators
class Blah
{
public:

Blah();

~Blah();

operator int();
};


This came from VC++.NET; when I moved his code into it and tried to
compile, it complained that the "function (name) differs from (name)
only by return type." He had declared it as void, and the output window
said that the function definition had an int return type. Another VC++
slipup?


It must be a backward compatibility extension of the compiler. In the
old old days of C we could leave out the return type of functions and
the return type would be 'int'. Not in the later standard C; certainly
not in C++...

Ali
Jul 22 '05 #7
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:pU***************@newsread1.dllstx09.us.to.ve rio.net...
Stephen Tyndall wrote:
"Ruben Van Havermaet" <rv******@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...
class SomeClass
{
public:
SomeClass(Strategy s);

Style: constructors and destructors are inline no matter what you do, so


Where did you get this nonsense?


Actually, I don't know. Reading my post again, there's some stuff there
that's really stupid, and I don't understand why it was even there. I had
just gotten out of bed (I know, ridiculously late), so I really should have
given it some more time before I posted. I just wasn't thinking
clearly...obviously a constructor is called when an object is created, but
it's never inline.
it's usually best just to put the constructor/destructor definitions inside the class like this:

SomeClass(Strategy s) : s_(s) { }

Also, initializing a data member like this has no effect.
What are you talking about?


You're thinking I said that the initialization list didn't initialize
anything, and it's true I could have been more clear. I meant to say that
the class object data member couldn't be initialized like this because
there's no data inside the object to be initialized.
The private
member variable Strategy s_ is already a Strategy. "Initializing" it with a StrategyA has no effect.


Are you out of your mind?


True, the StrategyA object could hold data to initialize the Strategy
object. But this wasn't what he was doing.
There is no data that needs to be initialized, and


What if I later add data to be initialised?


We're not talking about you here. We're talking about his code; he didn't
have any data to initialize.
you can't initialize a object with another object anyway.


What????


Crap, that was stupid. Why was that there? All through the post, I see
places where I used general terms instead of saying it only applied to his
code.

//apologetically,
//mike tyndall
Jul 22 '05 #8
"Ali Cehreli" <ac******@yahoo.com> wrote in message
news:pa*********************************@yahoo.com ...
On Tue, 03 Aug 2004 12:34:19 -0700, Stephen Tyndall wrote:
"JKop" <NU**@NULL.NULL> wrote in message
news:mC******************@news.indigo.ie...
Stephen Tyndall posted:

> If you don't supply a return type, int is assumed. Since this
> function returns no value, void is required.

Incorrect.

There are three and only three instances in which you may not specify a
return type (and in all three instances it is compulsory that you
don't):

a) Constructors

b) Destructor

c) Type conversion operators
class Blah
{
public:

Blah();

~Blah();

operator int();
};


This came from VC++.NET; when I moved his code into it and tried to
compile, it complained that the "function (name) differs from (name)
only by return type." He had declared it as void, and the output window
said that the function definition had an int return type. Another VC++
slipup?


It must be a backward compatibility extension of the compiler. In the
old old days of C we could leave out the return type of functions and
the return type would be 'int'. Not in the later standard C; certainly
not in C++...


Any idea how I would turn that off?

//mike tyndall
Jul 22 '05 #9
On Tue, 3 Aug 2004 14:47:50 -0500, Stephen Tyndall <sw*******@hotmail.com>
wrote:
>
> This came from VC++.NET; when I moved his code into it and tried to
> compile, it complained that the "function (name) differs from (name)
> only by return type." He had declared it as void, and the output

window
> said that the function definition had an int return type. Another

VC++
> slipup?


It must be a backward compatibility extension of the compiler. In the
old old days of C we could leave out the return type of functions and
the return type would be 'int'. Not in the later standard C; certainly
not in C++...


Any idea how I would turn that off?

//mike tyndall


I would try 'disable language extensions'. I'd don't know if it works in
this case but its certainly worth having on. Along with other beauties
like 'treat wchar_t as built in type', 'force conformance in for loop
scope', and 'enable run time type info'. With all those you'll have a
reasonably standard conformant compiler. And make sure you've upgraded to
7.1

john
Jul 22 '05 #10
"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsb59pix9212331@andronicus...
On Tue, 3 Aug 2004 14:47:50 -0500, Stephen Tyndall <sw*******@hotmail.com>
wrote:
>
> This came from VC++.NET; when I moved his code into it and tried to
> compile, it complained that the "function (name) differs from (name)
> only by return type." He had declared it as void, and the output
window
> said that the function definition had an int return type. Another
VC++
> slipup?

It must be a backward compatibility extension of the compiler. In the
old old days of C we could leave out the return type of functions and
the return type would be 'int'. Not in the later standard C; certainly
not in C++...


Any idea how I would turn that off?

I would try 'disable language extensions'. I'd don't know if it works in
this case but its certainly worth having on. Along with other beauties
like 'treat wchar_t as built in type', 'force conformance in for loop
scope', and 'enable run time type info'. With all those you'll have a
reasonably standard conformant compiler. And make sure you've upgraded to
7.1


Thanks. I switched all of those on.

//mike tyndall
Jul 22 '05 #11
John Harrison wrote:

I would try 'disable language extensions'. I'd don't know if it works
in this case but its certainly worth having on. Along with other
beauties like 'treat wchar_t as built in type', 'force conformance in
for loop scope', and 'enable run time type info'. With all those you'll
have a reasonably standard conformant compiler. And make sure you've
upgraded to 7.1

john


Does the Holy Standard specify wchar_t as a builtin?
Also, for those who must include <windows.h> (ugh!), disabling language
extensions is not an option.

My second sentence is waaaay OT, so I'll stop here.
Jul 22 '05 #12
On Tue, 03 Aug 2004 21:09:55 GMT, red floyd <no*****@here.dude> wrote:
John Harrison wrote:
I would try 'disable language extensions'. I'd don't know if it works
in this case but its certainly worth having on. Along with other
beauties like 'treat wchar_t as built in type', 'force conformance in
for loop scope', and 'enable run time type info'. With all those
you'll have a reasonably standard conformant compiler. And make sure
you've upgraded to 7.1
john


Does the Holy Standard specify wchar_t as a builtin?


Yes it does, its a type in it own right, but Visual C++ compilers treat it
as a typedef (for unsigned short I think).

john
Jul 22 '05 #13
John Harrison wrote:
On Tue, 03 Aug 2004 21:09:55 GMT, red floyd <no*****@here.dude> wrote:
John Harrison wrote:
I would try 'disable language extensions'. I'd don't know if it
works in this case but its certainly worth having on. Along with
other beauties like 'treat wchar_t as built in type', 'force
conformance in for loop scope', and 'enable run time type info'.
With all those you'll have a reasonably standard conformant
compiler. And make sure you've upgraded to 7.1
john


Does the Holy Standard specify wchar_t as a builtin?

Yes it does, its a type in it own right, but Visual C++ compilers treat
it as a typedef (for unsigned short I think).

john


Thanks.
Jul 22 '05 #14
Stephen Tyndall wrote:
"Ruben Van Havermaet" <rv******@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...
Hi,

I have a problem using member functions in derived classes that
override virtual member functions of base classes.

The following pieces of (simplified) code don't work. Can anybody
give me some hints on what might be wrong?

// BEGIN OF SAMPLE CODE
You know this, but I'm going to go ahead and put it here:

#include <iostream>
using std::cout;
class Strategy
{
public:
Strategy() {}
virtual void run() { cout<<"Please override!"; }
};

class StrategyA: public Strategy
{
public:
StrategyA() {}
void run();


Style: the run() function above is virtual; it's best to go ahead and
say "virtual" to remind you what it is.
};
void StategyA::run()


void StrategyA::run()
{
cout <<"Functionality A";
}
class SomeClass
{
public:
SomeClass(Strategy s);


Style: constructors and destructors are inline no matter what you do,


Huh? Who told you that? It's wrong.
so it's usually best just to put the constructor/destructor
definitions inside the class like this:

SomeClass(Strategy s) : s_(s) { }
It's as good or bad as putting any member function directly into the
class definition.
Also, initializing a data member like this has no effect. The private
member variable Strategy s_ is already a Strategy. "Initializing" it
with a StrategyA has no effect.
Well, there is an effect, but not the desired one.
There is no data that needs to be initialized, and you can't
initialize a object with another object anyway.
Yes, you can.
Change your constructor to:

SomeClass() { }
The above line is useless. You can just leave the whole constructor out
instead. The compiler will automatically generate the above one.

void doSomething();

private:
Strategy s_;
SomeClass' data member is a Strategy, not a StrategyA. If you changed
this to

StrategyA s_;

Then it will work.


That however won't bring the OP any closer to polymorphic behaviour.
};

SomeClass::SomeClass(Strategy s): s_(s) {}


Erase the line above.

SomeClass::doSomething()


void SomeClass::doSomething()

If you don't supply a return type, int is assumed. Since this
function returns no value, void is required.
{
s_.run();
}
int main()
{
StategyA s;


This should be

StrategyA s;

except that it shouldn't be there.
SomeClass c(s);


Change this to

SomeClass c;

After all these changes, the program should work.


That depends on your definition of "should work". It might compile and
do something, but for sure not what was intended.

c.doSomething();
This will print "Functionality A" now.

return 0;
}
// END OF SAMPLE CODE
The above program will print out "Please override!" instead of
"Functionality A". Scanning through Stroustrup I can't seem to find
what is wrong.

An additional remark: I know it is better to declare the function
"run" in Strategy to be pure virtual (virtual void run() = 0;) but
that won't compile. The compiler gives error in SomeClass'
constructor and private member declaration. What do I forget?


Declaring a pure virtual function makes the class an ADT (abstract
data type). Because you cannot create an object of an ADT, it must
have a default constructor (but that's already there).


Nonsense.
The problem comes from trying to give SomeClass a Strategy member.
When you do this, a Strategy object is "constructed" inside SomeClass,
which is an error.
Right. The class cannot be instantiated, because it's abstract.
The SomeClass constructor error is basically the same; it comes from
trying to initialize an object of the Strategy ADT.

Here's my version of your code. It should have everything working.

#include <iostream>

using std::cout;

class Strategy

{

public:

Strategy() {}

virtual void run() { cout<<"Please override!"; }

};

class StrategyA: public Strategy

{

public:

StrategyA() {}

virtual void run();

};

void StrategyA::run()

{

cout <<"Functionality A\n";

}

class SomeClass

{

public:

SomeClass() { }

void doSomething();

private:

StrategyA s_;

};

void SomeClass::doSomething()

{

s_.run();

}

int main()

{

SomeClass c;

c.doSomething();

return 0;

}


Jul 22 '05 #15
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:ce*************@news.t-online.com...
Stephen Tyndall wrote:
Style: constructors and destructors are inline no matter what you do,
Huh? Who told you that? It's wrong.


Yeah, I know. I don't know what I was thinking. I originally thought that
"inline function" meant "function not explicitly called by the programmer"
(like a constructor/destructor, or sort of like an operator) and was kind of
confused for a while. That's been cured now, but I apparently had a relapse
: )
Also, initializing a data member like this has no effect. The private
member variable Strategy s_ is already a Strategy. "Initializing" it
with a StrategyA has no effect.


Well, there is an effect, but not the desired one.
There is no data that needs to be initialized, and you can't
initialize a object with another object anyway.


Yes, you can.
Change your constructor to:

SomeClass() { }


The above line is useless. You can just leave the whole constructor out
instead. The compiler will automatically generate the above one.


Yeah, I know. I tend to go ahead and put it there in my code anyway, even
though it's pointless.
An additional remark: I know it is better to declare the function
"run" in Strategy to be pure virtual (virtual void run() = 0;) but
that won't compile. The compiler gives error in SomeClass'
constructor and private member declaration. What do I forget?


Declaring a pure virtual function makes the class an ADT (abstract
data type). Because you cannot create an object of an ADT, it must
have a default constructor (but that's already there).


Nonsense.


What do you mean? My beginner's book a while back made it clear several
times that having a pure virtual function makes a class an ADT. Do you mean
the thing about the constructor?

BTW, Victor Bazarov caught some other mistakes in my post that you may or
may not have seen. I was very groggy this morning : )

//mike tyndall
Jul 22 '05 #16
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:1Y********************@comcast.com...
Declaring a pure virtual function makes the class an ADT (abstract
data type). Because you cannot create an object of an ADT, it must
have a default constructor (but that's already there).
Nonsense.


What do you mean? My beginner's book a while back made it clear several
times that having a pure virtual function makes a class an ADT. Do you

mean the thing about the constructor?


I must admit, that constructor comment does sound like nonsense. To me, it
makes about as much sense as saying that the class must be named Fred. You
were quite groggy, weren't you? ;-)

--
David Hilsee
Jul 22 '05 #17
"David Hilsee" <da*************@yahoo.com> wrote in message
news:56********************@comcast.com...
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:1Y********************@comcast.com...
> Declaring a pure virtual function makes the class an ADT (abstract
> data type). Because you cannot create an object of an ADT, it must
> have a default constructor (but that's already there).

Nonsense.
What do you mean? My beginner's book a while back made it clear several
times that having a pure virtual function makes a class an ADT. Do you

mean
the thing about the constructor?


I must admit, that constructor comment does sound like nonsense. To me,

it makes about as much sense as saying that the class must be named Fred. You were quite groggy, weren't you? ;-)


Heh :)
Don't worry, next time I'll take the time to go over my post a few times
(and maybe use the word "time" a little less).

//mike tyndall
Jul 22 '05 #18
Victor Bazarov <v.********@comAcast.net> wrote in message news:<oW**************@newsread1.dllstx09.us.to.ve rio.net>...
Ruben Van Havermaet wrote:
I have a problem using member functions in derived classes that
override virtual member functions of base classes.

The following pieces of (simplified) code don't work. Can anybody give
me some hints on what might be wrong?


What is wrong is that you are trying to achieve polymorphic behaviour
without using pointers or references. That's rather impossible.

I quite agree with what Victor says. The problem is just becasue the
op is trying to get the polymorphic behaviour. I tried the same code
with pointers(references would create problem for this particular
code) and I found the code is running perfectly fine.

#include <iostream>

using namespace std ;

class Base {
public:
Strategy() {}
virtual void run() { cout<<"Please override!"<<endl; }
} ;

class Derived : public Base {
public:
Derived() {}
virtual void run() { cout <<"Functionality A"<<endl; };
} ;

class Someclass {
private:
Base* b_ ;
public:
Someclass(Base* b) : b_(b){ }
void doSomething() { b_->run(); }
} ;

int main() {
Base* a = new Base();
Derived* d = new Derived();
Someclass s(a) ;
Someclass s1(d);
s.doSomething() ;
s1.doSomething() ;
return 0 ;
}
------------------------------------------
output:
Please override!
Functionality A
------------------------------------------
I think this is what op wanted to clarify.

pls point out , if I am wrong somewhere.

pankaj
Jul 22 '05 #19
Victor Bazarov <v.********@comAcast.net> wrote in message news:<oW**************@newsread1.dllstx09.us.to.ve rio.net>...

What is wrong is that you are trying to achieve polymorphic behaviour
without using pointers or references. That's rather impossible.


I guess that forgetting to mention the word 'polymorphism' made my
post somewhat inclear. :)

Thanks, Victor, I got it working now. If you're used to dynamically
typed languages, c++ turns out to be quite a challenge :)
Declaring the Strategy class to be abstract now also works. I assume
that this is because

Strategy& strat

in the private member declaration of SomeClass, doesn't need to be
constructed.

Friendly Greetings
Ruben.
Jul 22 '05 #20
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message news:<6d********************@comcast.com>...

SomeClass' data member is a Strategy, not a StrategyA. If you changed this
to

StrategyA s_;

Then it will work.
But what if I would want to use some other Strategy? What I really
intended to find out was how to implement "late binding of a function
call" or polymorphism.
(Stroustrup, "The C++ Programming Language", p. 312). The key to this,
as Victor pointed out, is to use references and pointers.

Declaring a pure virtual function makes the class an ADT (abstract data
type). Because you cannot create an object of an ADT, it must have a
default constructor (but that's already there). The problem comes from
trying to give SomeClass a Strategy member. When you do this, a Strategy
object is "constructed" inside SomeClass, which is an error. The SomeClass
constructor error is basically the same; it comes from trying to initialize
an object of the Strategy ADT.


But making it a reference clears this problem out of the way.
Friendly Greetings,
Ruben.
Jul 22 '05 #21

"Ruben Van Havermaet" <rv******@yahoo.co.uk> wrote in message
news:54**************************@posting.google.c om...

The above program will print out "Please override!" instead of
"Functionality A". Scanning through Stroustrup I can't seem to find
what is wrong.
You can only have polymorphism with dynamic binding. In other words, all
your variables (created objects) are created statically and passed by value.
So the compiler will never defer the function calls to runtime. You would
need to use pointers or references to use polymorphism.
An additional remark: I know it is better to declare the function
"run" in Strategy to be pure virtual (virtual void run() = 0;) but
that won't compile. The compiler gives error in SomeClass' constructor
and private member declaration. What do I forget?


You are passing the type by value. When you pass by value, you create a new
copy of the object. How can you create a new copy of a Strategy object if
the class is an abstract class? Again, you'd have to pass by pointer or
reference so the compiler doesn't try to make a copy of the object.
Jul 22 '05 #22

"Ruben Van Havermaet" <rv******@yahoo.co.uk> wrote in message
news:54*************************@posting.google.co m...

Declaring the Strategy class to be abstract now also works. I assume
that this is because

Strategy& strat

in the private member declaration of SomeClass, doesn't need to be
constructed.


That and the fact that you're not passing it by value in the function call.
Jul 22 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
2
by: Bill Zhao | last post by:
Dear all, Sorry for i did not paste original propreity codes See below the fake codes for what I cannot understand. =============================================== lass A { public: virtual...
9
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. --...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
2
by: tony | last post by:
Hi! A derived class can override a method in the base class it inherits for, and even my dog knows that. More incredibly, I know and understand it too. But, can a (of course virtual in this...
5
by: dj | last post by:
I somehow understand why member function templates cannot be virtual, but my question is how I could achieve something similar to this: class A { public: template<typename Tvirtual f() = 0; }...
6
by: Ole Nielsby | last post by:
Given a class hierarchy: class A{public: virtual void m();}; class B : public A {public: virtual void m();}; class C : public B {public: virtual void m();}; //overrides m() class D : public B...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
3
by: Leon | last post by:
Hi, I am trying to design a class, which calls all a specific virtual function from all it's subclasses. Like this: class Base { public: virtual void theFunction() { // stuff goes here }; };...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.