-3

I need a class like that:

class MyClass
{
  MyClass()
  {
  }

  public:
  MyClass(signed = 0)
  {
  }
}

I'm wondering if it's possible to call these constructors selectively. Actually what I need is to have two versions of constructors. One to create object with default value like second constructor in my example and the other one to create a default/raw/uninitialized object without any parameters which is used internally in the same class.

I can do following to get over this problem:

class MyClass
{
  MyClass(float)
  {
  }

  public:
  MyClass(signed = 0)
  {
  }
}

And to call the first constructor inside my class but it's weird to have unused parameter.

OmegaDoom
  • 23
  • 5
  • `BigInt()` isn't a constructor of `MyClass`. You'll get a compiler error with that code. – πάντα ῥεῖ May 03 '16 at 16:13
  • `BigInt` is not a constructor of this class, its a member function and needs a return value. – Biruk Abebe May 03 '16 at 16:13
  • Yes, my mistake. Corrected it. – OmegaDoom May 03 '16 at 16:14
  • Possible duplicate of [What is the proper way to call default constructors?](http://stackoverflow.com/questions/20278781/what-is-the-proper-way-to-call-default-constructors) – sjsam May 03 '16 at 16:16
  • A default constructor is the constructor of a class that can be called without any arguments. So i believe you will still get a compiler error when you try to create an object of this class because in your case you have 2 default constructors. – Biruk Abebe May 03 '16 at 16:16
  • I don't think you can since overload resolution is done before checking access so in all case a `MyClass()` will result in ambiguous overload. – Holt May 03 '16 at 16:16
  • If you're using C++11 or higher, you can using [delegating constructors](http://stackoverflow.com/questions/308276/call-constructor-from-constructor-in-c) with the exact code you posted above. – paulsm4 May 03 '16 at 16:19
  • Yes, exactly, it has ambiguity and compiler doesn't allow such code but i don't need exact solution i have posted. What I need is to achieve my goal. To be able to construct some kind of raw/uninitialized object with help of other constructor which is visible only to the same class in order to initialize its members separately. – OmegaDoom May 03 '16 at 16:22
  • @OmegaDoom: look at delegating constructors: https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/introduction_to_the_c_11_feature_delegating_constructors?lang=en. Alternatively, you can have *one* constructor with a combination of 1) default parameters, and 2) common initialization code. Please post back what you find. – paulsm4 May 03 '16 at 16:25
  • AFAIK delegating constructors allow to call one constructor inside another but I don't need it. I need different constructors but with similar signatures. I can make them different in signature too but it looks weird. For example it's possible to make a private constructor that gets float and use it like raw constructor but it would be definitely weird solution. – OmegaDoom May 03 '16 at 16:29
  • Guys, your suggested solution doesn't have anything in common with my problem. I don't have a problem to define or call default constructor. Please, read more carefully what my problem is about. – OmegaDoom May 03 '16 at 16:53

1 Answers1

0

You have a few options. Your "work-around" isn't crazy, and in fact it's a pattern that is encouraged in some circumstances. But consider the named-constructor idiom instead: If you need to create an uninitialized object in a member function of the same class, then create a named private c'tor that achieves this for you:

class MyClass {
public:
  MyClass(float f = 0.) : value_{f} {}
private:      
  float value_;

  void g();
  MyClass invalid() 
  { 
    return MyClass{std::numeric_limits<float>::max()}; }
};

That way, inside of MyClass::g(), you can do:

void MyClass::g()
{
  auto newObj = invalid(); // call named c'tor
  newObj.value_ = 3.14159;
  std::swap(newObj, *this) // swap current object with 'newObj'
  // or whatever.
}
KyleKnoepfel
  • 1,426
  • 8
  • 24
  • But it still calls MyClass(float) which I want to avoid. I don't see how it solves this problem. I've created nested private structure and defined private constructor with it. It looks better but in my opinion strange anyway. – OmegaDoom May 03 '16 at 18:55
  • There's no way around that. The compiler cannot know that you're calling the c'tor whose argument has a default value vs. a c'tor that receives no arguments. According to C++, either one is a default c'tor...and there must be only one. The resolution to your problem is to either use the one-and-only default c'tor for both cases, or to remove the default value for the one-argument c'tor. – KyleKnoepfel May 03 '16 at 20:21
  • I know it's not possible in 03 and probably in C++11 standard too but I was wondering if it's changed in new standard or maybe there is a better solution than mine. Bu anyway thank you for reassuring me that there is no such thing. Only one thing. I see some people are very fast at their decision to judge questions without really understanding them. Now I can't post a question anymore because these people didn't like this one. – OmegaDoom May 04 '16 at 18:31