Re: Trouble with non-default constructors

From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 07/11/04


Date: Sat, 10 Jul 2004 22:11:29 -0400


"Peter Venis" <petervenis@zonnet.nl> wrote in message
news:6d317$40eff9c2$3ea61746$1242@news2.zonnet.nl...
> Hi,
>
> I have a class that needs a non-default constructor, the class is supposed
> to look like this:
>
> class MyClass
> {
> protected:
> int Value;
> public:
> MyClass(int V);
> ~MyClass();
> void GetValue(){return Value);
> };
>
> But if I try to compile my compilers complains there's no default
> constructor, but I don't want a default constructor (???)

compile what?

>
> To try and hack myself around this problem I do this:
>
> class MyClass
> {
> protected:
> int Value;
> public:
> MyClass(){} // Why can't I leave out this obsolete line of code?
> MyClass(int V);

this cstor should be:
    MyClass(const int v) : Value(v) { }
or
    MyClass(const int& r_v) : Value(r_v) { }

The fact you ignored the initialization list/ member construction is
probably why the compiler is begging for a default constructor.

> ~MyClass();
> void GetValue(){return Value);

you return a value and yet the return type is void. see below.

> };
>
> Is there a better way to solve this?
>
> And there's another problem, when I create a class that's derived from
> MyClass, something like class MyChildClass : public MyClass, then I cannot
> use the non-default constructor for that class becuase the compiler says
it
> does not exist (??). What am I doing wrong?

Nobody knows, you've not shown how the class is used and therefore nobody
can deduce whether a default constructor is required or whether a copy
constructor is invoked.

Whats certain is that providing no default constructor does work in the case
one isn't required. Here is an example:

#include <iostream>

class MyClass
{
    // members
    int m_value;
    // lifecycle
public:
    MyClass(const int v) : m_value(v) { std::cout << " cstor invoked\n"; }
    ~MyClass() { std::cout << " d~stor invoked\n\n"; }
    MyClass(const MyClass& r_c) : m_value(r_c.m_value)
    {
        std::cout << " copy cstor invoked\n";
    }
    // member functions
    int& getValue() { return m_value; }
};

int main()
{
    MyClass myObject(101);

    std::cout << "\n myObject's variable is ";
    std::cout << myObject.getValue() << std::endl << std::endl;

    return 0;
}

Notice how each cstor/d~stor/member function announces thats it was invoked
or called.



Relevant Pages

  • Class Destroys itself straight away!
    ... I have a Class who's constructor accepts an STL wstring, but it seems to kill itself before it can be used. ... class MyClass ... void MyClass::SayHello ...
    (microsoft.public.vc.language)
  • Trouble with non-default constructors
    ... I have a class that needs a non-default constructor, ... class MyClass ... int Value; ... But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to share data?
    ... > int getdata; ... Why is readdata() public? ... Your constructor copies the values ...
    (comp.programming)
  • Re: Object instantiation in C#
    ... >> I have a class MyClass that throws exceptions. ... > When an UNCAUGHT exception occurs in a constructor, ... > pointing to whatever it was pointing to before. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trouble with non-default constructors
    ... In article, Peter Venis ... >I have a class that needs a non-default constructor, ... >class MyClass ... >But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)