support bazel complie this project and format code.
This commit is contained in:
51
basic_content/sizeof/BUILD
Normal file
51
basic_content/sizeof/BUILD
Normal file
@@ -0,0 +1,51 @@
|
||||
# please run `bazel run basic_content/sizeof:blackclass`
|
||||
# please run `bazel run basic_content/sizeof:genA`
|
||||
# please run `bazel run basic_content/sizeof:geninhe`
|
||||
# please run `bazel run basic_content/sizeof:moreinhe`
|
||||
# please run `bazel run basic_content/sizeof:morevir`
|
||||
# please run `bazel run basic_content/sizeof:static`
|
||||
# please run `bazel run basic_content/sizeof:virinhe`
|
||||
# please run `bazel run basic_content/sizeof:virmoreinhe`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "blackclass",
|
||||
srcs = ["blackclass.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "genA",
|
||||
srcs = ["genA.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "geninhe",
|
||||
srcs = ["geninhe.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "moreinhe",
|
||||
srcs = ["moreinhe.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "static",
|
||||
srcs = ["static.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "virinhe",
|
||||
srcs = ["virinhe.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "virmoreinhe",
|
||||
srcs = ["virmoreinhe.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
@@ -6,14 +6,11 @@
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include<bits/stdc++.h>
|
||||
#include <bits/stdc++.h>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include<graphics.h> // use size in graphics also
|
||||
class A{};
|
||||
int main()
|
||||
{
|
||||
cout<<sizeof(A)<<endl;
|
||||
cout<<sizeof
|
||||
return 0;
|
||||
class A {};
|
||||
int main() {
|
||||
cout << sizeof(A) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,29 +1,27 @@
|
||||
/**
|
||||
* @file genA.cpp
|
||||
* @brief 普通成员函数,大小为1,一个类中,虚函数本身、成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空间。
|
||||
* @brief
|
||||
* 普通成员函数,大小为1,一个类中,虚函数本身、成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空间。
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A();
|
||||
~A();
|
||||
static int a;
|
||||
static void fun3();
|
||||
void fun();
|
||||
void fun1();
|
||||
class A {
|
||||
public:
|
||||
A();
|
||||
~A();
|
||||
static int a;
|
||||
static void fun3();
|
||||
void fun();
|
||||
void fun1();
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<sizeof(A)<<endl; // 1
|
||||
return 0;
|
||||
int main() {
|
||||
cout << sizeof(A) << endl; // 1
|
||||
return 0;
|
||||
}
|
||||
|
@@ -8,15 +8,14 @@
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
char a;
|
||||
int b;
|
||||
class A {
|
||||
public:
|
||||
char a;
|
||||
int b;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -34,39 +33,32 @@ class A
|
||||
* long b
|
||||
* 根据字节对齐2+2+4+8=16
|
||||
*/
|
||||
class B:A
|
||||
{
|
||||
public:
|
||||
short a;
|
||||
long b;
|
||||
class B : A {
|
||||
public:
|
||||
short a;
|
||||
long b;
|
||||
};
|
||||
/**
|
||||
* 把A的成员拆开看,char为1,int为4,所以是1+(3)+4+1+(3)=12,()为字节补齐
|
||||
*/
|
||||
class C
|
||||
{
|
||||
A a;
|
||||
char c;
|
||||
* 把A的成员拆开看,char为1,int为4,所以是1+(3)+4+1+(3)=12,()为字节补齐
|
||||
*/
|
||||
class C {
|
||||
A a;
|
||||
char c;
|
||||
};
|
||||
|
||||
class A1
|
||||
{
|
||||
virtual void fun(){}
|
||||
};
|
||||
class C1:public A
|
||||
{
|
||||
class A1 {
|
||||
virtual void fun() {}
|
||||
};
|
||||
class C1 : public A {};
|
||||
|
||||
int main() {
|
||||
cout << sizeof(A) << endl; // 8
|
||||
cout << sizeof(B) << endl; // 16
|
||||
cout << sizeof(C) << endl; // 12
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<sizeof(A)<<endl; // 8
|
||||
cout<<sizeof(B)<<endl; // 16
|
||||
cout<<sizeof(C)<<endl; // 12
|
||||
|
||||
/**
|
||||
* @brief 对于虚单函数继承,派生类也继承了基类的vptr,所以是8字节
|
||||
*/
|
||||
cout<<sizeof(C1)<<endl; // 8
|
||||
return 0;
|
||||
/**
|
||||
* @brief 对于虚单函数继承,派生类也继承了基类的vptr,所以是8字节
|
||||
*/
|
||||
cout << sizeof(C1) << endl; // 8
|
||||
return 0;
|
||||
}
|
||||
|
@@ -6,38 +6,33 @@
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
char a;
|
||||
int b;
|
||||
class A {
|
||||
public:
|
||||
char a;
|
||||
int b;
|
||||
};
|
||||
|
||||
class B
|
||||
{
|
||||
public:
|
||||
short a;
|
||||
long b;
|
||||
class B {
|
||||
public:
|
||||
short a;
|
||||
long b;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 8+16+8=32
|
||||
*/
|
||||
class C:A,B
|
||||
{
|
||||
char c;
|
||||
class C : A, B {
|
||||
char c;
|
||||
};
|
||||
|
||||
int main() {
|
||||
cout << sizeof(A) << endl; // 8
|
||||
cout << sizeof(B) << endl; // 16
|
||||
cout << sizeof(C) << endl; // 32
|
||||
|
||||
int main()
|
||||
{
|
||||
cout<<sizeof(A)<<endl; // 8
|
||||
cout<<sizeof(B)<<endl; // 16
|
||||
cout<<sizeof(C)<<endl; // 32
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
@@ -6,19 +6,18 @@
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A{
|
||||
class A {
|
||||
|
||||
virtual void fun();
|
||||
virtual void fun1();
|
||||
virtual void fun2();
|
||||
virtual void fun3();
|
||||
virtual void fun();
|
||||
virtual void fun1();
|
||||
virtual void fun2();
|
||||
virtual void fun3();
|
||||
};
|
||||
int main()
|
||||
{
|
||||
cout<<sizeof(A)<<endl;
|
||||
return 0;
|
||||
int main() {
|
||||
cout << sizeof(A) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,33 +1,30 @@
|
||||
/**
|
||||
* @file static.cpp
|
||||
* @brief 静态数据成员
|
||||
* 静态数据成员被编译器放在程序的一个global data members中,它是类的一个数据成员,但不影响类的大小。不管这个类产生了多少个实例,还是派生了多少新的类,静态数据成员只有一个实例。静态数据成员,一旦被声明,就已经存在。
|
||||
* 静态数据成员被编译器放在程序的一个global data
|
||||
* members中,它是类的一个数据成员,但不影响类的大小。不管这个类产生了多少个实例,还是派生了多少新的类,静态数据成员只有一个实例。静态数据成员,一旦被声明,就已经存在。
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class A
|
||||
{
|
||||
public:
|
||||
char b;
|
||||
virtual void fun() {};
|
||||
static int c;
|
||||
static int d;
|
||||
static int f;
|
||||
class A {
|
||||
public:
|
||||
char b;
|
||||
virtual void fun(){};
|
||||
static int c;
|
||||
static int d;
|
||||
static int f;
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
/**
|
||||
* @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8
|
||||
*/
|
||||
cout << sizeof(A) << endl;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8
|
||||
*/
|
||||
cout<<sizeof(A)<<endl;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
@@ -6,33 +6,28 @@
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
virtual void fun() {}
|
||||
class A {
|
||||
virtual void fun() {}
|
||||
};
|
||||
|
||||
class B
|
||||
{
|
||||
virtual void fun2() {}
|
||||
class B {
|
||||
virtual void fun2() {}
|
||||
};
|
||||
class C : virtual public A, virtual public B
|
||||
{
|
||||
public:
|
||||
virtual void fun3() {}
|
||||
class C : virtual public A, virtual public B {
|
||||
public:
|
||||
virtual void fun3() {}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
int main()
|
||||
{
|
||||
/**
|
||||
* @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr
|
||||
*/
|
||||
cout << sizeof(A) << " " << sizeof(B) << " " << sizeof(C);
|
||||
|
||||
/**
|
||||
* @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr
|
||||
*/
|
||||
cout<<sizeof(A)<<" "<<sizeof(B)<<" "<<sizeof(C);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
@@ -6,33 +6,28 @@
|
||||
* @date 2019-07-21
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
virtual void fun() {}
|
||||
class A {
|
||||
virtual void fun() {}
|
||||
};
|
||||
|
||||
class B
|
||||
{
|
||||
virtual void fun2() {}
|
||||
class B {
|
||||
virtual void fun2() {}
|
||||
};
|
||||
class C : public A, public B
|
||||
{
|
||||
public:
|
||||
virtual void fun3() {}
|
||||
class C : public A, public B {
|
||||
public:
|
||||
virtual void fun3() {}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
int main()
|
||||
{
|
||||
/**
|
||||
* @brief 8 8 16 派生类继承多个虚函数,会继承所有虚函数的vptr
|
||||
*/
|
||||
cout << sizeof(A) << " " << sizeof(B) << " " << sizeof(C);
|
||||
|
||||
/**
|
||||
* @brief 8 8 16 派生类继承多个虚函数,会继承所有虚函数的vptr
|
||||
*/
|
||||
cout<<sizeof(A)<<" "<<sizeof(B)<<" "<<sizeof(C);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user