Re: Trouble with non-default constructors
From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 07/11/04
- Next message: Val: "Proper deletion question."
- Previous message: Ben Cottrell: "Re: Default constructors, passing argument"
- In reply to: Peter Venis: "Trouble with non-default constructors"
- Next in thread: jeffc: "Re: Trouble with non-default constructors"
- Reply: jeffc: "Re: Trouble with non-default constructors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Val: "Proper deletion question."
- Previous message: Ben Cottrell: "Re: Default constructors, passing argument"
- In reply to: Peter Venis: "Trouble with non-default constructors"
- Next in thread: jeffc: "Re: Trouble with non-default constructors"
- Reply: jeffc: "Re: Trouble with non-default constructors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|