调用构造函数?

编程入门 行业动态 更新时间:2024-10-12 01:23:44
本文介绍了调用构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任何人都知道调用构造函数的方法吗? 首先,我要发布以下代码并询问它是否完全 便携式和所有,但它不会编译。 #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

更多推荐

调用构造函数?

本文发布于:2023-11-12 09:53:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!