Skip to main content
edited body
Source Link
user4442671
user4442671

I am interpreting the question as "how to executeperform non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

// in ContainerObject.cpp

#include <stdexcept> // for std::invalid_argument

// Anonymous namespace since main_factory() is only needed in this TU.
namespace {

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {}

I am interpreting the question as "how to execute non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

// in ContainerObject.cpp

#include <stdexcept> // for std::invalid_argument

// Anonymous namespace since main_factory() is only needed in this TU.
namespace {

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {}

I am interpreting the question as "how to perform non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

// in ContainerObject.cpp

#include <stdexcept> // for std::invalid_argument

// Anonymous namespace since main_factory() is only needed in this TU.
namespace {

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {}

added 49 characters in body
Source Link
user4442671
user4442671

I am interpreting the question as "how to execute non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

// in ContainerObject.cpp

#include <stdexcept> // for std::invalid_argument

// Anonymous namespace since main_factory() is only needed in this TU.
namespace {

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {

 } 

I am interpreting the question as "how to execute non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

#include <stdexcept>

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {

 }

I am interpreting the question as "how to execute non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

// in ContainerObject.cpp

#include <stdexcept> // for std::invalid_argument

// Anonymous namespace since main_factory() is only needed in this TU.
namespace {

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {} 

added 18 characters in body
Source Link
user4442671
user4442671

I am interpreting the question as "how to execute non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

#include <stdexcept>

MainClass foomain_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainObject"MainClass");
}

ContainerObject::ContainerObject(int type) 
  : MainObject(foomain_factory(type)) {

}

I am interpreting the question as "how to execute non-trivial logic before the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

MainClass foo(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  throw std::invalid_argument("invalid type for MainObject");
}

ContainerObject::ContainerObject(int type) 
  : MainObject(foo(type)) {

}

I am interpreting the question as "how to execute non-trivial logic before/during the member initialization list".

A good way to go about that is to delegate the work of converting the constructor parameter of the outer object into the child object to a utility function:

#include <stdexcept>

MainClass main_factory(int type) {
  if (type == 0) {
    return MainClass("something", 1, 2);
  } else if (type == 1) {
    return MainClass(4, "another thing", "yet another thing");
  }

  // N.B. This is one of the scenarios where exceptions are indisputably
  // the best way to do error handling.
  throw std::invalid_argument("invalid type for MainClass");
}

ContainerObject::ContainerObject(int type) 
  : MainObject(main_factory(type)) {

}
added 8 characters in body
Source Link
user4442671
user4442671
Loading
added 202 characters in body
Source Link
user4442671
user4442671
Loading
added 202 characters in body
Source Link
user4442671
user4442671
Loading
Source Link
user4442671
user4442671
Loading