Re: Class Destroys itself straight away!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi David, Alex,

Thanks for the solution. Are there any articles/tutorials on this?

I looked at the code here

http://www.cplusplus.com/doc/tutorial/classes.html

Is it because they are passing numeric constants direct that it doesn't create a copy?

// example: class constructor
#include <iostream>
using namespace std;

class CRectangle {
int width, height;
public:
CRectangle (int,int);
int area () {return (width*height);}
};

CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}

int main () {
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}

David Wilkinson wrote:
Gerry Hickman wrote:
Hi,

I have a Class who's constructor accepts an STL wstring, but it seems to kill itself before it can be used. If I initialize with int, it works as expected. Here's the sample code and the output. Note the Destructor is firing twice.

#include "stdafx.h"
using namespace std;

class MyClass
{
public:
MyClass(wstring);
~MyClass(void);
void SayHello(void);
};

int _tmain(int argc, _TCHAR* argv[])
{
wstring wsInput = _T("Passed in String");
MyClass mc = MyClass(wsInput);
// Class kills itself here!
wcout << "About to use Class" << endl;
mc.SayHello();
return 0;
}

MyClass::MyClass(wstring wsNewString)
{
wcout << "Constructing Class" << endl;
}

MyClass::~MyClass(void)
{
wcout << "Destructing Class" << endl;
}

void MyClass::SayHello()
{
wcout << "Say Hello" << endl;
}

// Output From Program

Constructing Class
Destructing Class <-- WHY??
About to use Class
Say Hello
Destructing Class

Gerry:

This is happening because you are making unnecessary copies. Try like this:

class MyClass
{
public:
MyClass(const wstring&);
~MyClass(void);
void SayHello(void);
};

int _tmain(int argc, _TCHAR* argv[])
{
wstring wsInput = L"Passed in String";
MyClass mc(wsInput);
wcout << L"About to use Class" << endl;
mc.SayHello();
return 0;
}

Always pass input parameters by const reference if you can.

You can also do just

MyClass mc(L"Passed in String");

BTW, you should not use _T("") with wstring; use L"". If you want "build-agnostic" code you can do things like

typdef std::basic_string<TCHAR> tstring;



--
Gerry Hickman (London UK)
.



Relevant Pages

  • Re: Explicit function vs. overloaded operator?
    ... > void operator() (int x, ... The conceptual advantage is that if class MyClass does just one thing, ... make the code easier to read. ...
    (comp.lang.cpp)
  • Re: basic newbie question about types
    ... void MyMember(); ... Now I have an object t that represents MyClass. ...
    (microsoft.public.dotnet.general)
  • Re: This I simply cant swallow
    ... encapsulation and abstract data types are completely orthogonal ... int i; ... MyClass i; //An instance of MyClass ... An ADT is a type that cannot, itself, be instantiated. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Why this fix work ?
    ... "int" is 32 bits wide. ... This is basically a matter of luck -- bad luck that it works at ... MyClass* myobj; ... "MyClass *", which is actually 64 bits wide. ...
    (comp.lang.c)
  • Re: Template collection classes
    ... But it also says as it's true, that we quite always use Reference as ... void Func(int x) ... MyClass(): x ... you will read the message "MyClass copy ctor ...
    (microsoft.public.vc.mfc)