调用构造函数? [英] Calling a constructor?
问题描述
任何人都知道调用构造函数的方法吗?
首先,我要发布以下代码并询问它是否完全
便携式和所有,但它不会编译。
#include< cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = * static_cast< AnyClass *>
(std :: malloc(sizeof(AnyClass)));
blah.AnyClass();
//正常使用
blah .~AnyClass();
std :: free(& blah);
}
G ++给出了愚蠢的回复:
mem.cpp:在函数`int main()''中:
mem.cpp:14:调用类型`AnyClass''就像一个方法
有没有人知道编译器对错误提供有意义的解释
和警告?它不一定要产生任何东西,只需
分析代码并给出错误和警告,以及一个有用的
解释。
-JKop
JKop发布:
任何人都知道调用构造函数的方法吗?
首先,我要发布以下内容代码并询问
如果它是完全可移植的全部,但它不会编译。
#include< cstdlib>
class AnyClass {} ;
int main()
{
AnyClass& blah = * static_cast< AnyClass *>
(std :: malloc(sizeof(AnyClass)));
blah.AnyClass();
//正常工作
blah .~AnyClass();
std :: free(& blah);
}
G ++给出了愚蠢的回复:
mem.cpp:在函数`int main()''中:
mem.cpp:14:调用类型`AnyClass''之类的方法
有没有人知道编译器为错误和警告提供有意义的
解释?它并不一定要产生任何东西,只需分析即可。代码并给出错误和
警告,以及一个有用的解释。
-JKop
如果我能得到它以某种方式工作,它是否适用于具有多态函数的
类?
-JKop
JKop发布:
JKop发布:
任何人都知道调用构造函数的方法吗?
首先,我打算发布下面的代码并询问
,如果它是完全可移植的,但是它不会编译。
#include< cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = * static_cast< AnyClass *>
(std :: malloc(sizeof(AnyClass)));
blah.AnyClass();
//正常工作
blah .~AnyClass();
std :: free(& blah);
}
G ++给出了愚蠢的回复:
mem.cpp:在函数`int main()''中:
mem.cpp:14:调用类型`AnyClass''之类的方法
有没有人知道编译器为错误和警告提供有意义的
解释?它并不一定要产生任何东西,只需分析即可。代码并给出错误和
警告,以及一个有用的解释。
-JKop
如果我能以某种方式工作,它会与具有多态函数的类一起工作吗?
-JKop
更正
虚拟功能
-JKop
* JKop写道:
任何人都知道一种方式调用构造函数?
首先,我将发布以下代码,并询问它是否完全可移植等等,但它无法编译。
#include< cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = * static_cast< AnyClass *>
(std :: malloc(sizeof(AnyClass)));
blah.AnyClass();
AnyClass& blah = * new(std :: malloc(sizeof(AnyClass)))AnyClass;
-
Robert Bauck Hamar
Anyone know a way of calling a constructor?
At first, I was going to post the following code and ask if it was fully
portable and all, but then it wouldn''t compile.
#include <cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = *static_cast<AnyClass*>
( std::malloc( sizeof(AnyClass) ) );
blah.AnyClass();
//Work with it as normal
blah.~AnyClass();
std::free(&blah);
}
G++ gives the stupid reply:
mem.cpp: In function `int main()'':
mem.cpp:14: calling type `AnyClass'' like a method
Does anyone know of a compiler that gives meaningful explanations for errors
and warnings? It doesn''t necessarily have to produce anything, just
"analyzes" the code and gives the errors and warnings, along with a helpful
explanation.
-JKop
JKop posted:
Anyone know a way of calling a constructor?
At first, I was going to post the following code and ask if it was fully portable and all, but then it wouldn''t compile.
#include <cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = *static_cast<AnyClass*>
( std::malloc( sizeof(AnyClass) ) );
blah.AnyClass();
//Work with it as normal
blah.~AnyClass();
std::free(&blah);
}
G++ gives the stupid reply:
mem.cpp: In function `int main()'':
mem.cpp:14: calling type `AnyClass'' like a method
Does anyone know of a compiler that gives meaningful explanations for errors and warnings? It doesn''t necessarily have to produce anything, just "analyzes" the code and gives the errors and warnings, along with a helpful explanation.
-JKop
If I could get that to somehow work, would it work with
classes that have polymorphic functions?
-JKop
JKop posted:
JKop posted:
Anyone know a way of calling a constructor?
At first, I was going to post the following code and ask if it was fully portable and all, but then it wouldn''t compile.
#include <cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = *static_cast<AnyClass*>
( std::malloc( sizeof(AnyClass) ) );
blah.AnyClass();
//Work with it as normal
blah.~AnyClass();
std::free(&blah);
}
G++ gives the stupid reply:
mem.cpp: In function `int main()'':
mem.cpp:14: calling type `AnyClass'' like a method
Does anyone know of a compiler that gives meaningful explanations for errors and warnings? It doesn''t necessarily have to produce anything, just "analyzes" the code and gives the errors and warnings, along with a helpful explanation.
-JKop
If I could get that to somehow work, would it work with
classes that have polymorphic functions?
-JKop
CORRECTION
virtual functions
-JKop
* JKop wrote:
Anyone know a way of calling a constructor?
At first, I was going to post the following code and ask if it was fully
portable and all, but then it wouldn''t compile.
#include <cstdlib>
class AnyClass {};
int main()
{
AnyClass& blah = *static_cast<AnyClass*>
( std::malloc( sizeof(AnyClass) ) );
blah.AnyClass();
AnyClass& blah = *new (std::malloc(sizeof(AnyClass))) AnyClass;
--
Robert Bauck Hamar
这篇关于调用构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!