64,639
社区成员
发帖
与我相关
我的任务
分享
mystring(char*) = 1 1
------------------------------
mystring(char*) = base 1
mystring(char*) = NULL 1
myfunc8_base : default constructor is necessary in parent class
mystring(char*) = base2 1
mystring operator = base2 3
mystring(char*) = base2 1
myderived : myderived(mystring&) : m_sf8 = 1 4
Press any key to continue . . .
// ConsoleApplication15.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class mystring
{
public:
mystring(char* p = NULL)
{
cout << "mystring(char*) = " << (NULL != p ? p : "NULL") << " "<< 1<<endl;
if(NULL == p)
{
m_pdata = new char[1];
*m_pdata = 0;
}
else
{
int ilen = strlen(p);
m_pdata = new char[ilen + 1];
strcpy(m_pdata, p);
}
}
~mystring()
{
if(NULL != m_pdata)
{
delete []m_pdata;
m_pdata = NULL;
}
}
mystring(const mystring& obj)
{
cout << "mystring(mystring&) = " << obj.m_pdata <<" "<< 2<< endl;
mystring(obj.m_pdata);
}
const mystring& operator = (const mystring& rhs)
{
cout << "mystring operator = " << rhs.m_pdata << " "<<3<<endl;
mystring(rhs.m_pdata);
return *this;
}
public:
char* m_pdata;
};
class mybase
{
public:
virtual ~mybase(){};
mybase() : m_sf8("base")
{
cout << "myfunc8_base : default constructor is necessary in parent class" << endl;
m_sf82 = "base2";
}
private:
mystring m_sf8;
mystring m_sf82;
};
class myderived : public mybase
{
public:
myderived(mystring& sf8) : m_sf8(sf8)
{
cout << "myderived : myderived(mystring&) : m_sf8 = " << m_sf8.m_pdata << " "<< 4<<endl;
}
private:
const mystring& m_sf8;
};
int main(int argc, char* argv[])
{
mystring ms81("1");
myderived m8d2(ms81);
return 0;
}
#include <iostream>
using namespace std;
class mystring
{
public:
mystring(char* p = NULL)
{
cout << "mystring(char*) = " << (NULL != p ? p : "NULL") << endl;
if(NULL == p)
{
m_pdata = new char[1];
*m_pdata = 0;
}
else
{
int ilen = strlen(p);
m_pdata = new char[ilen + 1];
strcpy(m_pdata, p);
}
}
~mystring()
{
if(NULL != m_pdata)
{
delete []m_pdata;
m_pdata = NULL;
}
}
mystring(const mystring& obj)
{
cout << "mystring(mystring&) = " << obj.m_pdata << endl;
mystring(obj.m_pdata);
}
const mystring& operator = (const mystring& rhs)
{
cout << "mystring operator = " << rhs.m_pdata << endl;
mystring(rhs.m_pdata);
return *this;
}
public:
char* m_pdata;
};
class mybase
{
public:
virtual ~mybase(){};
mybase() : m_sf8("base")
{
cout << "myfunc8_base : default constructor is necessary in parent class" << endl;
m_sf82 = "base2";
}
private:
mystring m_sf8;
mystring m_sf82;
};
class myderived : public mybase
{
public:
myderived(mystring& sf8) : m_sf8(sf8)
{
cout << "myderived : myderived(mystring&) : m_sf8 = " << m_sf8.m_pdata << endl;
}
private:
const mystring& m_sf8;
};
int main(int argc, char* argv[])
{
mystring ms81("1");
myderived m8d2(ms81);
return 0;
}