覆盖构造函数 [英] Overriding a constructor
问题描述
我正在尝试用我的代码解决问题,但是不能提出一个
解决方案。
假设我有一个类一个带有它的构造函数(我不能直接修改
)
函数A(){
/ *构造函数代码* /
};
我想做的是让构造函数调用某个函数我在返回之前指定
,即
函数A(){
/ *构造函数代码* /
someFunction();
};
这可能吗?
CAFxX
只是为了澄清,目的是所有代码实例化一个类的
实例。 A,执行对someFunction()的调用而不重写。
CAFxX ha scritto:
我正在尝试解决我的代码问题,但不能提出一个
的解决方案。
假设我有一个类一个带有它的构造函数(我不能直接修改
)
函数A(){
/ *构造函数代码* /
};
我想做的是让构造函数调用某个函数我
在返回之前指定,即
函数A(){
/ *构造函数代码* /
someFunction();
};
这可能吗?
CAFxX
CAFxX写道:
我正试图用我的代码解决问题但是不能想出一个
解决方案。
假设我有一个类一个带有它的构造函数(我不能直接修改
)
函数A(){
/ *构造函数代码* /
};
我想做的是让构造函数调用某个函数我
在返回之前指定,即
函数A(){
/ *构造函数代码* /
someFunction();
};
这可能吗?
CAFxX
为什么不使用扩展第一类的第二类。在第二个
类中你可以有一个方法A,在它的构造函数中你可以有
this-> A();
I''m trying to solve a problem with my code but can''t come up with a
solution.
Suppose I have a "class" A with its constructor (that I can''t modify
directly)
function A() {
/* constructor code */
};
what I''d like to do is to make the constructor call a certain function I
specify before returning, i.e.
function A() {
/* constructor code */
someFunction();
};
is this possible?
CAFxX
Just to clarify, the aim is that all the code that is instantiating an
instance of "class" A, perform the call to someFunction() without rewrites.
CAFxX ha scritto:I''m trying to solve a problem with my code but can''t come up with a
solution.
Suppose I have a "class" A with its constructor (that I can''t modify
directly)
function A() {
/* constructor code */
};
what I''d like to do is to make the constructor call a certain function I
specify before returning, i.e.
function A() {
/* constructor code */
someFunction();
};
is this possible?
CAFxX
CAFxX wrote:I''m trying to solve a problem with my code but can''t come up with a
solution.
Suppose I have a "class" A with its constructor (that I can''t modify
directly)
function A() {
/* constructor code */
};
what I''d like to do is to make the constructor call a certain function I
specify before returning, i.e.
function A() {
/* constructor code */
someFunction();
};
is this possible?
CAFxXWhy not have a second class that extends the first class. In the second
class you could have a method A and in its constructor you could have
this->A();
这篇关于覆盖构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!