"琐碎"模板方法模式的问题 [英] "trivial" problem with template method pattern

查看:64
本文介绍了"琐碎"模板方法模式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,当我开始实现它时,我认为它应该是简单易用的b $ b。我不确定以下

代码有什么问题:


A级

{

public:

A(){}

int get()

{

int a;

得到(a);

返回a;

}

受保护:

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(); //这是一个问题,我不知道为什么

}


出于某种原因,我不明白编译器不能找到

继承了methode" int get()"。我使用g ++版本4.1.2 20070925.

解决方案



" rogo" < d。******** @ velian.dewrote in message

news:11 ********************* @ o3g2000hsb.googlegrou ps.com ...


好​​的,当我开始实现它时,我认为它应该是直接的b / b
简单。我不确定以下

代码有什么问题:


A级

{

public:

A(){}

int get()

{

int a;

得到(a);

返回a;

}

受保护:

virtual void get(int&)= 0;

};



当编译器到达此处时,它会看到A所拥有的一切,并且没有

:: get(int)或A ::得到(INT)。换句话说,get()可以调用

范围内没有get(int)。


继承类可以调用基类中的函数,但是基类可以

通常不会在派生类中调用函数。


>

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(); //这是一个问题,我不知道为什么

}


由于某种原因,我还不明白编译器不能找到

继承了methode" int get()"。我使用g ++版本4.1.2 20070925.



rogo写道:


好吧,当我开始实现它时,我认为它应该是简单易用的b $ b。我不确定以下

代码有什么问题:


A级

{

public:

A(){}

int get()

{

int a;

得到(a);

返回a;

}

受保护:

virtual void get(int&)= 0;

};


class B:public A

{

public:

B(int a):b(a){}



使用A :: get();


private:

void get(int& a)

{

a = b;

}

int b;

};


int main(){

B b(1);

b.get(); //这是一个问题,我不知道为什么

}


出于某种原因,我不明白编译器不能找到

继承了methode" int get()"。我使用g ++版本4.1.2 20070925.



行为是正确的。 B :: get(int&)的声明隐藏了B的get()的所有其他

实例。你有两个选择:在你的所有你的声明中使用声明

子类(见上文),或重命名get(int&)。第二个

是一个更好的解决方案,特别是因为get(int&)是私有的。


2007年10月31日星期三09:06: 22 -0700,Jim Langston

< ta ******* @ rocketmail.comwrote:


>继承的类可以从基类调用函数,但是基类通常不能在派生类中调用函数。



我想我对你的措辞提出质疑;肯定

整个_point_的虚函数,例如,允许

派生类替换或扩充由

提供的实现基类,即基类使用

派生类调用函数?

-

PGP密钥ID 0xEB7180EC


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.

解决方案


"rogo" <d.********@velian.dewrote in message
news:11*********************@o3g2000hsb.googlegrou ps.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.



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.


On Wed, 31 Oct 2007 09:06:22 -0700, "Jim Langston"
<ta*******@rocketmail.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


这篇关于&QUOT;琐碎&QUOT;模板方法模式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆