0

In calling Parent's constructor from Child's constructor, how do I first instantiate a AnotherClass instance based on myString below, then pass in that AnotherClass instance to Parent's constructor? If that's not possible, what is a common pattern to achieve this in C++ ?

class Parent {
   public Parent(AnotherClass myClass){
      //....
   }
}

class Child : public Parent{
  public Child (std::string myString) : Parent (// get a AnotherClass instance by passing in myString and use that instance as input to Parent constructor) {}
}

class AnotherClass{
  public AnotherClass(std::string myString){
    ///
  }
}
user1008636
  • 2,989
  • 11
  • 31
  • 45
  • @FrançoisAndrieux sorry, i meant: assuming the constructor to `AnotherClass` takes in a std::string. So I just want to get an instance of `AnotherClass` by passing in `MyString`, then pass that instance as input to `Parent`'s constructor – user1008636 Aug 29 '22 at 20:32
  • 1
    Assuming you add missing colons, semicolons and fix the order of class declartions, it should work with just `Child (std::string myString) : Parent (myString) {}` (`AnotherClass` constructor is not `explicit`, so compiler can implicitly convert `std::string` to `AnotherClass` when needed). – Yksisarvinen Aug 29 '22 at 20:37
  • @Yksisarvinen but `Parent`s constructor takes in a `AnotherClass` instance, and `AnotherClass` 's constructor takes in a `myString`. So I need to get a `AnotherClass` instance first, then send it to the `Parent`'s constructor – user1008636 Aug 29 '22 at 20:39
  • 2
    Compiler can do it automatically, since you are using non-`explicit` constructor: https://godbolt.org/z/5P9E8rK8d. You can create a temporary object yourself with `: Parent(AnotherClass(myString))`, but it's not necessary. This should be explained in evvery [beginner C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Yksisarvinen Aug 29 '22 at 20:42
  • what if `AnotherClass(myString)` turns out to be very long or complex, that i need a separate function for? cc @Yksisarvinen – user1008636 Aug 29 '22 at 20:43
  • 2
    I'd seriously recommend getting a good C++ book. You can't learn this language by asking random questions or by trial and error. To answer your question: as long as the function returns `AnotherClass` or something convertible (`std::string` in this case), there is no issue in using it: https://godbolt.org/z/55GxMY79h *Although you can't use `virtual` functions and a couple other things, all should be out of scope for you now. – Yksisarvinen Aug 29 '22 at 20:55
  • By the way, your code is ¾ C++ and ¼ C#... In C++ access specifier ends while `:` and apply until next access specifieir. Also in C++, a class needs to eng with a `;`. As already recommanded by other, you should read a good book on C++ at this point. There is simply to many basic and simple things you don't know and C++ really is a language that need proper understanding to be used properly. – Phil1970 Aug 29 '22 at 21:02

1 Answers1

0

Easy solution: just do nothing. Compiler does it for you (as written in comments), as long as it's not an explicit ctor for AnotherClass:

class Child : public Parent
{
  public Child(std::string myString)
  : Parent(myString) {}
};

You can even consider simply using the ctor of Parent (by simply writing using Parent::Parent), albeit that changes the interface.

If you'd like to be verbose, you might say (again, as in the comments):

class Child : public Parent
{
  public Child(std::string myString)
  : Parent(AnotherClass(myString)) {}
};

These are for simple cases. However, sometimes you need to solve a more complicated issue before calling Parent's ctor: e.g., you'd like to reuse AnotherClass or do some calculation/verification on it. This is why I wrote the answer: in the generic case, you might need to do some arbitrary complex calculation. You might still do that, with the help of lambdas (or static member functions, or even free functions):

class Child : public Parent
{
  public Child(std::string myString)
  : Parent([&](){
    // here you can write any code
    // it'll run _before_ Parent's ctor
    AnotherClass ac(myString);
    // do any calculation on ac, store it, etc.
    return ac;
  }()) {}
};

Also, suggest using std::move when you pass the argument (unless, of course, when you need it in another place in the ctor as well).

lorro
  • 10,687
  • 23
  • 36