473,770 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"trivial" problem with template method pattern

Ok, when I began to implement it, I thought it should be
straightforward and easy. I'm not sure whats wrong with the following
code:

class A
{
public:
A() {}
int get()
{
int a;
get(a);
return a;
}
protected:
virtual void get(int&) = 0;
};

class B : public A
{
public:
B(int a) : b(a) {}
private:
void get(int& a)
{
a = b;
}
int b;
};

int main() {
B b(1);
b.get();// thats a problem, and I dont know why
}

For some reason I dont understand yet the compiler can't "find" the
inherited methode "int get()". I use g++ version 4.1.2 20070925.

Oct 31 '07 #1
11 2174

"rogo" <d.********@vel ian.dewrote in message
news:11******** *************@o 3g2000hsb.googl egroups.com...
Ok, when I began to implement it, I thought it should be
straightforward and easy. I'm not sure whats wrong with the following
code:

class A
{
public:
A() {}
int get()
{
int a;
get(a);
return a;
}
protected:
virtual void get(int&) = 0;
};
When the complier gets here it sees everything that A has, and there is no
::get(int) or A::get(int). In other words, there is no get(int) within
scope that get() can call.

Inherited classes can call functions from base classes, but base classes can
not normally call functions within derived classes.
>
class B : public A
{
public:
B(int a) : b(a) {}
private:
void get(int& a)
{
a = b;
}
int b;
};

int main() {
B b(1);
b.get();// thats a problem, and I dont know why
}

For some reason I dont understand yet the compiler can't "find" the
inherited methode "int get()". I use g++ version 4.1.2 20070925.

Oct 31 '07 #2
rogo wrote:
Ok, when I began to implement it, I thought it should be
straightforward and easy. I'm not sure whats wrong with the following
code:

class A
{
public:
A() {}
int get()
{
int a;
get(a);
return a;
}
protected:
virtual void get(int&) = 0;
};

class B : public A
{
public:
B(int a) : b(a) {}
using A::get();
private:
void get(int& a)
{
a = b;
}
int b;
};

int main() {
B b(1);
b.get();// thats a problem, and I dont know why
}

For some reason I dont understand yet the compiler can't "find" the
inherited methode "int get()". I use g++ version 4.1.2 20070925.
Behavior is correct. The declaration of B::get(int&) hides all other
instances of get() for B. You have two choices: put a using declaration
in all your child classes (see above), or rename get(int&). The second
is a better solution, especially since get(int&) is private.
Oct 31 '07 #3
On Wed, 31 Oct 2007 09:06:22 -0700, "Jim Langston"
<ta*******@rock etmail.comwrote :
>Inherited classes can call functions from base classes, but base classes can
not normally call functions within derived classes.
I think I take issue with you regarding the phrasing of that; surely
the entire _point_ of virtual functions, for example, is to allow the
derived class to replace or augment the implementation provided by the
base class, i.e. the base class is calling functions withing the
derived class?
--
PGP key ID 0xEB7180EC
Oct 31 '07 #4
"Keith Willis" <me@privacy.net wrote in message
news:g2******** *************** *********@4ax.c om...
On Wed, 31 Oct 2007 09:06:22 -0700, "Jim Langston"
<ta*******@rock etmail.comwrote :
>>Inherited classes can call functions from base classes, but base classes
can
not normally call functions within derived classes.

I think I take issue with you regarding the phrasing of that; surely
the entire _point_ of virtual functions, for example, is to allow the
derived class to replace or augment the implementation provided by the
base class, i.e. the base class is calling functions withing the
derived class?
No. Because with a polymorphism you create a derived class, not a base
class. It is the derived class's functions you are calling not the base
classes.

He is trying to call a base class function that calls a derived classes
function, which is normally not done. A base class knows nothing of the
classes that derive from it. The derived classes know of the base class
they derive from.
Oct 31 '07 #5

Jim Langston schrieb:
A base class knows nothing of the
classes that derive from it. The derived classes know of the base class
they derive from.
That's not true. The declaration of "virtual void get(int&) = 0;"
ensures that subclasses have to implement the declared method. So the
base class knows a method implementation exists in all derived
classes.
He is trying to call a base class function that calls a derived classes
function, which is normally not done.
I object to that too. It's the concept of the Template Method Pattern
(1) combined with standard inheritance. It should work and in other
object oriented languages it does. So, why not in C++?

(1) Gamma, E. et alii (2004): Design Patterns Elements of Reusable
Object-Oriented Software, 31st Printing, Boston u.a.

Nov 1 '07 #6

red floyd schrieb:
Behavior is correct. The declaration of B::get(int&) hides all other
instances of get() for B. You have two choices: put a using declaration
in all your child classes (see above), or rename get(int&). The second
is a better solution, especially since get(int&) is private.
Your second solution is exactly what I did. But I'm still curious why
A::get() gets hidden for B. Is this behaviour standard compliant?

Nov 1 '07 #7
On Nov 1, 11:39 am, rogo <d.rogow...@vel ian.dewrote:
red floyd schrieb:
Behavior is correct. The declaration of B::get(int&) hides all other
instances of get() for B. You have two choices: put a using declaration
in all your child classes (see above), or rename get(int&). The second
is a better solution, especially since get(int&) is private.

Your second solution is exactly what I did. But I'm still curious why
A::get() gets hidden for B. Is this behaviour standard compliant?
A::get() is hidden within B's namespace, because C++ does not support
overloading across namespace scopes. Please see

http://www.research.att.com/~bs/bs_f...verloadderived

for further information.

--
For more than 4 generations the IT Professionals were the guardians
of quality and stability in software. Before the dark times.
Before Microsoft...

Nov 1 '07 #8
"rogo" <d.********@vel ian.dewrote in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
>
Jim Langston schrieb:
>A base class knows nothing of the
classes that derive from it. The derived classes know of the base class
they derive from.

That's not true. The declaration of "virtual void get(int&) = 0;"
ensures that subclasses have to implement the declared method. So the
base class knows a method implementation exists in all derived
classes.
>He is trying to call a base class function that calls a derived classes
function, which is normally not done.

I object to that too. It's the concept of the Template Method Pattern
(1) combined with standard inheritance. It should work and in other
object oriented languages it does. So, why not in C++?

(1) Gamma, E. et alii (2004): Design Patterns Elements of Reusable
Object-Oriented Software, 31st Printing, Boston u.a.
The question isn't what should work, but what does work. My statements are
applying to how C++ works, not how other languages work or how you think C++
should work. There is enough to dicuss in C++ currently without discussing
what shoulda, woulda, coulda been in it. comp.std.c++ is the forum to
discuss those issues.
Nov 1 '07 #9
On Nov 1, 11:39 am, rogo <d.rogow...@vel ian.dewrote:
red floyd schrieb:
Behavior is correct. The declaration of B::get(int&) hides
all other instances of get() for B. You have two choices:
put a using declaration in all your child classes (see
above), or rename get(int&). The second is a better
solution, especially since get(int&) is private.
Your second solution is exactly what I did. But I'm still
curious why A::get() gets hidden for B. Is this behaviour
standard compliant?
It's what the standard requires. The situation is somewhat
complicated by ADL, but in general, lookup works its way through
a series of scopes, stopping at the first scope where it finds
the symbol.

The reason for this simple. Consider the following code:

class Base
{
public:
virtual void f() = 0 ;

} ;
class Derived : public Base
{
public:
void f() { call g( 'a' ) ; }

private:
void g( int ) ;
} ;

Obviously, the implementation of f() wants to use the private
function g, which is part of the implementation. Now, what
will happen if Base is modified to add a new function:

class Base
{
public:
void g( char ) { f() ; }
}

Adding such a simple function to base shouldn't break the
derived classes, should it? (If name lookup didn't stop in
when it found the g in Derived, this change would result in
infinite recursion in Derived.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 2 '07 #10

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

Similar topics

13
29518
by: DarkSpy | last post by:
many c++ compilers including "gcc" have not implemented the "export" keyword, but the comeau compilers made it (just i knew). i want to know about: is it too difficult to implement "export" keyword? if it is, i know the history that is without "export" keyword of C++ compilers with five years (sorry about my poor english :-) ), in five years still have no some idea to implement it ? my project and many C++ programmers need this important...
205
10701
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
2
1536
by: Michael Olea | last post by:
Hiya. Frequently (as in at least twice now) I find that when I write some sort of container I want at least two out of three possible versions: o fixed max size known at compile time o fixed max size known at run time o max size is dynamic The question is how to name the variants consistently. My usual approach is
5
1478
by: Alfonso Morra | last post by:
Hi, I've written a very simple Simpleton design pattern class template. This was supposed to illustrate a point. Although it compiles without nay errors, I get link errors (unresolved external), during linking - before anyone jumps to conclusions that this is OT, please check the linker error message below, it is NOT OT. Here are my class declarations/definitions:
71
3184
by: Greg | last post by:
Is it your general opinion that C# is generally designed/intended/ready to replace C++? I know the answer is not black and white but please repond YES or NO (add any comments if you would like) I just thought it might be interesting to get a tally. This is related to a previous thread. So far we have two NOs from "aa" and Bo Persson. -- Greg McPherran www.McPherran.com
8
1840
by: Tomás | last post by:
I have a few really long template functions. They belong in a source file... and thus must be "exported". Seeing as how few compilers support "export", could someone please point me to a webpage/post that gives a very decent method of exploiting "export" for those compilers that *do* export it, while still remaining compatible with the defective compilers (which are in the majority!). I've seen in the comp.lang.c++ FAQ that you can do:
6
1476
by: Jeremy | last post by:
I understand what a unit test is. No problem there. What I'm wondering is what, specifically, do most of you more experienced developers mean by "writing" a unit test. Am I correct to believe that you are referring to some way you *document* your tests? If yes, can you provide some example? Given the huge amount of unit tests that go on with any non trivial application dev project I'd like to think that there is some standard template...
3
5859
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ section describes a solution using methods returning references to static objects. But consider:
3
2414
by: LionelAndJen | last post by:
I have an XML file that has a free form comment field in which the data provider, very kindly, already uses "&quot;" when writing "doesn't", I have doesn&apos;t . it's PERFECT, because that xml is then fed to an XSLT sheet that transforms this text into a sql insert statement. UNFORTUNATELY, XSLT translates doesn&apos;t into doesn't in the output, which then destroys my sql statement. How do I tell my XSLT to leave well-enough alone ? I...
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10058
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8886
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.