Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:52:50+00:00 2026-06-09T07:52:50+00:00

I have two classes: OuterClass and InnerClass. InnerClass is a private member of OuterClass

  • 0

I have two classes: OuterClass and InnerClass. InnerClass is a private member of OuterClass and should be created in OuterClass constructor with an InnerClass(int) constructor, however the default InnerClass constructor is still called.

InnerClass.hpp:

#ifndef INNERCLASS_HPP_
#define INNERCLASS_HPP_

class InnerClass {
public:
    int a;
    InnerClass();
    InnerClass(int);
    ~InnerClass();
};

#endif /* INNERCLASS_HPP_ */

InnerClass.cpp:

#include "InnerClass.hpp"
#include <iostream>

InnerClass::InnerClass() {
    a = 1;
    std::cout << "inner class constructed, a = " << a << std::endl;
}
InnerClass::InnerClass(int x) {
    a = x;
    std::cout << "inner class constructed, a = " << a << std::endl;
    //automatically: object InnerClass (a=3) is destroyed here...
}
InnerClass::~InnerClass() {
    std::cout << "inner class destructed, a = " << a << std::endl;
}

OuterClass.hpp:

#ifndef OUTERCLASS_HPP_
#define OUTERCLASS_HPP_

#include "InnerClass.hpp"

class OuterClass {
private:
    InnerClass blah;
public:
    OuterClass();
    ~OuterClass();
    void doSth();
};

#endif /* OUTERCLASS_HPP_ */

OuterClass.cpp:

#include "OuterClass.hpp"
#include <iostream>

OuterClass::OuterClass() {
    // automatically: blah = InnerClass();
    std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
    blah = InnerClass(3);
    std::cout << "outer class constructed" << std::endl;
}

OuterClass::~OuterClass() {
    std::cout << "outer class destructed" << std::endl;
}

void OuterClass::doSth() {
    std::cout << "doSth: " << blah.a << std::endl;
}

main:

#include "OuterClass.hpp"
#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Compiled at " << __TIME__ << std::endl;

    OuterClass x = OuterClass();
    x.doSth();

    std::cout << "done" << std::endl;
}

output:

Compiled at 12:11:12
inner class constructed, a = 1 //this is unexpected
outer class constructing started, blah.a = 1 //this should be random data
inner class constructed, a = 3
inner class destructed, a = 3 //this is unexpected
outer class constructed
doSth: 3
done
outer class destructed
inner class destructed, a = 3

Questions:

  1. Why is the default constructor of InnerClass called at the start of OuterClass constructor?
  2. What and why is destructed in OuterClass constructor (“inner class destructed, a = 3 //this is unexpected”)?
  3. It seems that InnerClass object with a = 3 was destructed in the OuterClass constructor, than why does method doSth() return 3 instead of random data?
  4. Why does removing an InnerClass() constructor (from both InnerClass.hpp and InnerClass.cpp files) result in compile-time error at the OuterClass constructor in OuterClass.cpp file? The error says that no InnerClass() definition found.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:52:51+00:00Added an answer on June 9, 2026 at 7:52 am

    Use initializer-list in constructor.

    OuterClass::OuterClass() : blah(3) {
        // automatically: blah = InnerClass();
        std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
        std::cout << "outer class constructed" << std::endl;
    }
    

    Since when you use

    OuterClass::OuterClass() {
        // automatically: blah = InnerClass();
        std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
        blah = InnerClass(3);
        std::cout << "outer class constructed" << std::endl;
    }
    

    firstly for initialize blah will be called default c-tor and in blah = InnerClass(3);, that creates temporary object and copy it to blah, after this string will be called destructor of temporary object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: public class Singleton{ private Singleton(){...} private static class InstanceHolder{ private
I have two classes called Person and Animal that have a lot of methods
I have two classes below: public class Module { public int Id { get;
I have two classes, Progression and FibonacciProgression, however FibonacciProgression print out some unexpected results
I have two classes: testClass and castClass : class testClass { public: int field1;
I have two classes public class PrepaidPackage { private String name; private String serviceClassID;
I have two classes public class A { public int BaseA {get;set;} } public
I have two classes, but don't what kind of relation i should use. I
I have two classes: class Order(models.Model): ... date = models.DateTimeField(blank=True, verbose_name=u'Date add',default=datetime.now) price =
I have two classes: public class Fighter { public int FighterID { get; set;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.