博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 中的一些错觉
阅读量:4361 次
发布时间:2019-06-07

本文共 1620 字,大约阅读时间需要 5 分钟。

1.

默认构造函数和不带参数的构造函数之间无联系

默认构造函数是编译器发现类不存在显式构造函数时自动生成的无参数的构造函数。同样,用户可以定义显示的无参数构造函数。

2.

在构造函数、析构函数中调用virtual 函数。并不会得到预期的结果。virtual函数在此时会"丢失"virtual性质。

3.

构造函数无参数的时候

1 #ifndef UNTITLED2_TEST_H 2 #define UNTITLED2_TEST_H 3  4 #include 
5 class test { 6 public: 7 test() 8 { 9 printf("call test()\n");10 }11 test(char c)12 {13 printf("call test(char)");14 }15 };16 17 18 #endif //UNTITLED2_TEST_H
1 #include "test.h"2 int main() {3     test t;//申明一个变量4     test tt();//声明一个函数。5     test tc('c'); //申明一个变量6     return 0;7 }
/Users/like1/CLionProjects/untitled2/cmake-build-debug/untitled2call test()call test(char)Process finished with exit code 0

test t();并不会像test t;调用test::test()构造函数,仅仅是申明了一个函数,返回类型是test,无参数,函数名是t。

4.

test tc = t;

test* t = &tc;

并不会调用operator=(test& t);而是调用test(test& t);

5.

new delete 和 malloc() free() 的行为存在共性,但不等价。

new 和 delete 会在分配内存后调用类的构造函数,析构函数。

6.

test t;test tt = t;

这样的定义语句不一定正确。

#ifndef UNTITLED2_UN_COPY_ASSIGN_H#define UNTITLED2_UN_COPY_ASSIGN_Hclass un_copy_assign {public:    un_copy_assign();private:    un_copy_assign(un_copy_assign& c);    un_copy_assign& operator=(un_copy_assign& c);};#endif //UNTITLED2_UN_COPY_ASSIGN_H
#ifndef UNTITLED2_TEST_H#define UNTITLED2_TEST_H#include "un_copy_assign.h"#include 
class test : public un_copy_assign{public: test():un_copy_assign() { printf("call test()\n"); } test(char c):un_copy_assign() { printf("call test(char)"); }};#endif //UNTITLED2_TEST_H

 

 

http://www.cnblogs.com/like1/p/6852528.html 

转载于:https://www.cnblogs.com/like1/p/6852528.html

你可能感兴趣的文章
大庆金桥帆软报表案例
查看>>
Proxy模式
查看>>
读书多些会怎样
查看>>
浏览器好用的技术
查看>>
HDU 2188------巴什博弈
查看>>
tp5任务队列使用supervisor常驻进程
查看>>
Xmind?
查看>>
spring+quartz 实现定时任务三
查看>>
day2-三级菜单
查看>>
linux下升级4.5.1版本gcc
查看>>
Beanutils
查看>>
FastJson
查看>>
excel4j
查看>>
Thread
查看>>
char * 与char []探究理解
查看>>
QT窗体显示在屏幕中间位置
查看>>
emmet使用技巧
查看>>
RPC-Thrift(二)
查看>>
MSSQL for Linux 安装指南
查看>>
【Golang 接口自动化08】使用标准库httptest完成HTTP请求的Mock测试
查看>>