Re: can I declare a class variable inside that class method?

From: Russell Hanneken (me_at_privacy.net)
Date: 06/15/04


Date: Tue, 15 Jun 2004 15:22:18 GMT

John Black wrote:
>
> Suppose I have a class,
>
> class MyClass{
> public:
> MyClass();
> MyClass(MyClass);

Suppose you wrote

     MyClass x;
     MyClass y(x);

Because the copy constructor parameter is passed by value, a copy of x
is made, and the copy is sent to the copy constructor. But to make the
copy, the MyClass copy constructor would have to be invoked. And then
the argument would have to be copied, using the MyClass copy constructor
again. And so on...

The point is, your copy constructor should take a reference, not a copy:

     MyClass(MyClass const &);

> int func();
> };
>
> Then can I declare a MyClass object inside MyClass::func()?
>
> int MyClass::func(){
> MyClass obj;
> <......>
> }

Sure.

-- 
Russell Hanneken
eunaarxra@cbobk.pbz
Use ROT13 to decode my email address.


Relevant Pages

  • Re: Variable declaration/definition
    ... class MyClass ... MyClass mc; ... Basically C# is closer to Java ... That calls the DateTime constructor, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Variable declaration/definition
    ... class MyClass ... MyClass mc; ... Basically C# is closer to Java than it is to C++ in many ways. ... That calls the DateTime constructor, but doesn't allocate anything on the heap, because DateTime is a value type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Class Destroys itself straight away!
    ... I have a Class who's constructor accepts an STL wstring, ... seems to kill itself before it can be used. ... MyClass mc = MyClass; ...
    (microsoft.public.vc.language)
  • Re: Author of Visual C++ 2005 STL ???
    ... static int count; ... to the copy constructor, ... so I think that only one copy ctor is required for the ... MyClass copy ctor ...
    (microsoft.public.vc.mfc)
  • Re: Singleton
    ... (new myClass()) can not be null|false|undefined.. ... As you are using a constructor to create this object instance it would ... be possible to define the created object's - constructor property on the ...
    (comp.lang.javascript)