0
点赞
收藏
分享

微信扫一扫

ConstructorInit.cpp


// ConstructorInit.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CTest{

	int m_test;
public:
	CTest(int t)
	{
		m_test=t;
		printf("CTest(int t) ,%d\n",t);
	}
	CTest& operator=(int t)
	{
		m_test=t;
		printf("CTest& operator=(int t) ,%d\n",t);
		return *this;
	}
	CTest& operator=(CTest& t)
	{
		m_test=t.m_test;
		printf("CTest& operator=(CTest& t) ,%d\n",t.m_test);
		return *this;
	}

};

class CExample {
public:
    CTest a;
    float b;
    //构造函数初始化列表
    CExample(): a(0),b(8.8)
    {}
    //构造函数内部赋值
	/*
    CExample()
    {
        a=0;
        b=8.8;
    }
	*/
};

int main(int argc, char* argv[])
{
	CExample ex;
	//printf("Hello World!\n");
	return 0;
}
/*
CTest(int t) ,0
Press any key to continue
*/

// Test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CTest{

	int m_test;
public:
	CTest()
	{
		m_test=-1;
		printf("CTest() ,%d\n",-1);
	}

	CTest(int t)
	{
		m_test=t;
		printf("CTest(int t) ,%d\n",t);
	}
	CTest& operator=(int t)
	{
		m_test=t;
		printf("CTest& operator=(int t) ,%d\n",t);
		return *this;
	}
	CTest& operator=(CTest& t)
	{
		m_test=t.m_test;
		printf("CTest& operator=(CTest& t) ,%d\n",t.m_test);
		return *this;
	}

};

class CExample {
public:
    CTest a;
    float b;
    //构造函数初始化列表
	/*
    CExample(): a(0),b(8.8)
    {}
	*/
    //构造函数内部赋值
	
    CExample()
    {
        a=0;
        b=8.8;
    }
	
};

int main(int argc, char* argv[])
{
	CExample ex;
	//printf("Hello World!\n");
	return 0;
}
/*
CTest() ,-1
CTest& operator=(int t) ,0
Press any key to continue
*/



// Test3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class CTest{

	int m_test;
public:
	CTest()
	{
		m_test=-1;
		printf("CTest() ,%d\n",-1);
	}

	CTest(int t)
	{
		m_test=t;
		printf("CTest(int t) ,%d\n",t);
	}
	/*
	CTest& operator=(int t)
	{
		m_test=t;
		printf("CTest& operator=(int t) ,%d\n",t);
		return *this;
	}
	*/
	CTest& operator=(CTest& t)
	{
		m_test=t.m_test;
		printf("CTest& operator=(CTest& t) ,%d\n",t.m_test);
		return *this;
	}

};

class CExample {
public:
    CTest a;
    float b;
    //构造函数初始化列表
	/*
    CExample(): a(0),b(8.8)
    {}
	*/
    //构造函数内部赋值
	
    CExample()
    {
        a=0;
        b=8.8;
    }
	
};

int main(int argc, char* argv[])
{
	CExample ex;
	//printf("Hello World!\n");
	return 0;
}
/*
--------------------Configuration: Test3 - Win32 Debug--------------------
Compiling...
Test3.cpp
E:\learn\program\vc\ConstructorInit\Test3\Test3.cpp(51) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
E:\learn\program\vc\ConstructorInit\Test3\Test3.cpp(52) : warning C4305: '=' : truncation from 'const double' to 'float'
执行 cl.exe 时出错.

Test3.exe - 1 error(s), 0 warning(s)
*/




举报

相关推荐

0 条评论