N
Ng Hun Yang
I have a class that has a default constructor that initializes the
class's variables. Now I want to add a new constructor to skip the
initialization.
My attempt:
class Integer {
public:
class NoInit { }; // newly added
public:
Integer(): v_(0) { }
Integer(int v): v_(v) { }
Integer(NoInit) { } // newly added
private:
int v_;
};
Here's how it's used:
class SomeClass {
public:
SomeClass() { }
private:
Integer a_;
};
I want SomeClass's constructor to skip initializing a_, so I changed it
to:
SomeClass(): a_(Integer(Integer::NoInit())) { }
Shouldn't the compiler do RVO, find that it's an empty object and
optimize the initialization away?
But I found that the compiler insisted on copying from the
uninitialized temporary variable to a_.
I read about using dummy classes/enum/types as constructor parameters
to distinguish between constructors, but I can't recall where I read
it.
What did I do wrong and what should I change to achieve my goal?
I can't change Integer's default constructor, because there is code
that depends on the value being initialized. However, SomeClass is
special because it never uses the default value.
Here's how SomeClass looks like:
class SomeClass {
public:
SomeClass() { }
public:
void Start(const Integer &a) { a_ = a; fn1(); fn2(); fn3(); }
private:
void fn1(); // does something with a_
void fn2(); // does something with a_
void fn3(); // does something with a_
private:
Integer a_;
};
Why not change SomeClass to:
class SomeClass {
public:
SomeClass(const Integer &a): a_(a) { }
public:
void Start() { fn1(); fn2(); fn3(); }
};
Well, that's not the question I'm asking, isn't it? :-D
btw, I'm using the Green Hill's C++ MIPS compiler; optimization is
turned on. The Multi suite is version 4.something, but I don't know the
actual compiler version.
class's variables. Now I want to add a new constructor to skip the
initialization.
My attempt:
class Integer {
public:
class NoInit { }; // newly added
public:
Integer(): v_(0) { }
Integer(int v): v_(v) { }
Integer(NoInit) { } // newly added
private:
int v_;
};
Here's how it's used:
class SomeClass {
public:
SomeClass() { }
private:
Integer a_;
};
I want SomeClass's constructor to skip initializing a_, so I changed it
to:
SomeClass(): a_(Integer(Integer::NoInit())) { }
Shouldn't the compiler do RVO, find that it's an empty object and
optimize the initialization away?
But I found that the compiler insisted on copying from the
uninitialized temporary variable to a_.
I read about using dummy classes/enum/types as constructor parameters
to distinguish between constructors, but I can't recall where I read
it.
What did I do wrong and what should I change to achieve my goal?
I can't change Integer's default constructor, because there is code
that depends on the value being initialized. However, SomeClass is
special because it never uses the default value.
Here's how SomeClass looks like:
class SomeClass {
public:
SomeClass() { }
public:
void Start(const Integer &a) { a_ = a; fn1(); fn2(); fn3(); }
private:
void fn1(); // does something with a_
void fn2(); // does something with a_
void fn3(); // does something with a_
private:
Integer a_;
};
Why not change SomeClass to:
class SomeClass {
public:
SomeClass(const Integer &a): a_(a) { }
public:
void Start() { fn1(); fn2(); fn3(); }
};
Well, that's not the question I'm asking, isn't it? :-D
btw, I'm using the Green Hill's C++ MIPS compiler; optimization is
turned on. The Multi suite is version 4.something, but I don't know the
actual compiler version.