Quantcast
Channel: Comments on: 11.4 — Constructors and initialization of derived classes
Browsing all 159 articles
Browse latest View live

By: Shraa

Hi I am learning CPP and I tried below code but it doesn’t work. #include <iostream> #include<string> using namespace std; class Base {     public:             int m_id;...

View Article


By: Alex

This is a tricky one. You’re declaring object dr with no parameters, but you’ve given it empty parenthesis. This will cause the compiler to think you’re declaring a function prototype for a function...

View Article


By: shraa

Thanks Alex but it wont work by removing empty parenthesis as then there will be ambiguous call to overloaded Derived (constructor)..In all way its giving me error.. Can’t I declare below two...

View Article

By: shraa

I just tried below and it worked: class Base {     public:             int m_id;             Base(int i=0):m_id(i){}             int getID() const { return m_id;} }; class Derived:public Base {...

View Article

By: Alex

The issue is that you’ve defined two default constructors, which causes ambiguity when you try to do something that requires a default constructor. In the latter case, there’s no ambiguity because...

View Article


By: Martin

Hi Alex, How would you do list initialization for base class? #include <array> using namespace std; class float3 : public array<float, 3>{}; int main() {    float3 mypos = { 1.0f, 2.0f,...

View Article

By: AMG

Alex, Could you please tell me if in my version "const" is redundant in class constructors and access functions. 1234567891011121314151617181920212223242526272829 class Fruit{private:  std::string...

View Article

By: Alex

You’ll have to define your own derived class constructor (in this case, in float3) that takes a std::initializer_list by const reference. If the base class has a constructor that takes a...

View Article


By: Alex

Nope, looks good.

View Article


By: Alexxx

About that part: Note that it doesn’t matter where in the Derived constructor initialization list the Base constructor is called -- it will always execute first. If i do 12     Derived(double cost=0.0,...

View Article

By: Alex

This warning will occur (for compilers that support it) any time you initialize class members in a different order than in which they are defined in the class (or, in this case, with the base class...

View Article

By: Ada

how is this? i used a different main function from the one given in the quiz #include<iostream> #include<string> using namespace std; class Fruit {     private:         string m_name;...

View Article

By: Alex

The code seems fine. But I did give you a main() function to use and you didn’t use it. 😐

View Article


By: xjuggy

I initially set my classes and member functions up in a similar fashion to yours, Alex, and all was well.  Then I decided to experiment with the "never reuse code" mantra: 1 out << "Apple ("...

View Article

By: xjuggy

Well, I made it to lesson 11.6a, and found the answer there: "static_cast"

View Article


By: Alex

There’s a better answer. Remove the overloaded operator<< functions for Apple and Banana. You don’t need them. Without them, Fruit::operator<&lt will be called, and that function already...

View Article

By: Ran

My code is less efficient than Alex's solution because I re-write getName() twice in the derived class. A better solution is to put getName() in the base class. I tried to do Alex's style, but I was...

View Article


By: nascardriver

Hi Ran! * @m_name is never initialized not used. * @Banana doesn't have a color on it's own, it's using the default value, it works, but it's not nice. * Uniform initialization is preferred. *...

View Article

By: Mau

Hello, thanks for the great tutorial and also a question. I got a bit confused by all those constructors being called, but despite that, in the end there is only one object on the call stack, isn't it?

View Article

By: nascardriver

Hi Mau! There are no objects on the call stack, there are addresses on the call stack. Assuming you're talking about regular objects, there's only one object, no matter how many constructors where...

View Article
Browsing all 159 articles
Browse latest View live