对C++对象实例化的测试
2024-08-24 14:14:29
#include <iostream>
using namespace std;
class class1 { public:
class1(){ }
class1(int i ){ }
void show(){ cout<<"this is output by class1 ! \n ";
}
};
int main(){
// static allocation :
/* class1 oneclass(); oneclass.show(); error : request show() function of non-class oneclass . because : compile thinks of oneclass1 oneclass() as of function declaration with name 'oneclass' and the return type class1 */
/*
class1 secondclass(1); secondclass.show();
*/
//dynamic allocation : class1 * thirdclass = new class1(); thirdclass->show(); return 0;
}
总结一下:
1. 当使用类的无参构造函数来实例化对象时,有两种方法:
类名 * x = new 类名();
类名 x;
2 . 当使用有参的构造函数时,有两种方法:
类名 * x = new 类名(参数列表);
类名 x(参数列表);
注 :编译器版本 :
最新文章
- css多行文本省略号问题
- 国内SEO如何过滤掉不良网络信息
- xcode armv6 armv7 armv7s arm64
- 能源项目xml文件标签释义--CommonsMultipartResolver
- 计算机原理学习(2)-- 存储器和I/O设备和总线
- Baskets of Gold Coins
- [COGS 2524]__完全平方数
- npm下载包很慢和node-sass编译错误的解决办法
- Apache启动不了httpd: apr_sockaddr_info_get() failed xgp
- Conway生命游戏
- Vim 安装 YouCompleteMe
- Exception in thread ";main"; java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
- python实例[判断操作系统类型]
- Eclipse CDT 插件列表
- [Docker] Building a Node.js Image
- mac pro 显示隐藏文件
- qt基本类
- zookeeper的安装及共享锁的应用
- [原][译][osgearth]Model Source Drivers模型驱动源(OE官方文档翻译)
- PHP——文件操作