Trouble with non-default constructors

From: Peter Venis (petervenis_at_zonnet.nl)
Date: 07/10/04


Date: Sat, 10 Jul 2004 16:14:26 +0200

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 (???)

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);
  ~MyClass();
  void GetValue(){return Value);
};

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?

Peter



Relevant Pages

  • 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
    ... > I have a class that needs a non-default constructor, ... > class MyClass ... > void GetValue{return Value); ... > But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)
  • 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)
  • class unable to write the memory
    ... class MyClass{ ... I call the class constructor in the Doc constructor, like I always do, but this time the constructor is unable to modify the memory ...
    (microsoft.public.vc.mfc)