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

  • SEARCH
  • Home
  • 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 7665197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:30:03+00:00 2026-05-31T14:30:03+00:00

I could imagine this question has already been asked, but I actually could not

  • 0

I could imagine this question has already been asked, but I actually could not find any fitting solution, so please excuse if this is a redundant question.

I have a custom class

class myClass_A
{
public:
    myClass_A();          // Constructor
    myFunction_A();       // Some function from Class A
};

Now I have another custom class which has a member from the type of myClass_A

class myClass_B
{
public:
    myFunction_B();       // Some function from Class B

private:
    myClass_A m_instance; // Instance of Class A
}

Now myFunction_B() wants to call the method myFunction_A() from m_instance kinda like this:

myClass_B::myFunction_B()
{
    m_instance.myFunction_A();
}

Now if I compile my code ( which is basically like the example I posted above ) it will succeed without any warnings or errors. So my questions would be:

A. Will the constructor be called in this example?

B. Can I actually call methods from an uninitialized object?

C. Assuming the constructor is not called but I can still call the methods from that object -> This means still that the members of my class are not initialized?

Sorry if these questions is kinda stupid but I feel like I am slow on the uptake right now.

  • 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-05-31T14:30:05+00:00Added an answer on May 31, 2026 at 2:30 pm

    These are very good and important questions.

    Regarding A:

    Before executing the body of your constructor, C++ generates code which automatically calls the default constructor of all aggregated (i.e. member) objects of your class. Basically, what it does is transform the following code:

    class myClass_B {
    public:
        myClass_B()
        {
            m_instance.foo();
            m_pInstance->foo();
        }
    private:
        myClass_A m_instance;
        myClass_A* m_pInstance;
    };
    

    into the following code:

    class myClass_B {
    public:
        myClass_B()
            : m_instance()
            , m_pInstance()
        {
            m_instance.foo();
            m_pInstance->foo();
        }
    private:
        myClass_A m_instance;
        myClass_A* m_pInstance;
    };
    

    The two lines the compiler automatically inserted are called initializer list, it calls the default constructor of each aggregate object before the body of your constructor is executed. Please note that the second one, m_pInstance() calls the “default constructor of pointer” which creates an uninitialized pointer ; this is almost always not what you want. See below on how to fix that.

    Now let’s assume that the constructor of myClass_A has the signature myClass_A(int someNumber), i.e. it takes an argument. Then, C++ cannot automatically generate the initializer list for myClass_B as it doesn’t know which number to pass myClass_A‘s constructor. It will throw a compiler error at you, probably complaining about a missing default constructor for myClass_A. You will have to write the initializer-list on your own, for example:

    class myClass_B {
    public:
        myClass_B()
            : m_instance(21)
            , m_pInstance(new myClass_A(21))
        {
            m_instance.foo();
            m_pInstance->foo();
        }
    private:
        myClass_A m_instance;
        myClass_A* m_pInstance;
    };
    

    This is correct code, which calls myClass_A constructor with the value 21 for the parameter someNumber. This also shows how you correctly initialize a pointer: make it point to some newly allocated object.

    Regarding B:

    Unlike some others say, you can! (Try it out)

    But it results in unexpected behaviour, which is not what you want. (Including that it might do what you want only when the planets are aligned correctly.) It will most probably crash but is not guaranteed to crash. This can lead you to some long debugging nights. If your compiler is smart, it might recognize this and warn you, but it will not give you an error.

    Also note that for non-pointer aggregate objects which have a default constructor, the default constructor will be called and you’ll be all good. The problem comes when you use builtin types or pointers. This is use of uninitialized variables, and is one of the most frequent causes for a bug. If your code does something totally weird, always check whether you initialized all your variables. It should become a reflex to put an entry in the initializer-list for any member-variable, even if it is calling the default constructor. Makes things clear.

    Regarding C:

    Yes. See B for details. The interesting thing is that if the method you call does not use the “this” pointer (this includes not using any attribute variable and not calling any method which uses an attribute variable), your method is guaranteed to work. What happens when you call a method on an uninitialized object is that the “this” object within the method (i.e. all attribute variables too) is random memory. The code of the method will execute but use random memory and this is what fails.

    I hope this clears things up a bit.

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

Sidebar

Related Questions

First, I know this question has been asked several times before and that in
Yep. This question came up looking at Code Igniter but it is not really
This question is part user experience, part engineering. I am trying to find a
Hey this may be a somewhat simple question, but I'm new to Eclipse and
I have an odd question that I have always thought about, but could never
This is an absolute beginner's question (sorry), but I was wondering how to commit
Background: I admit I did not attempt to benchmark this, but I'm curious... What
Forgive me if this is a silly question but I'm afraid that I don't
I've been pondering this question for a while: Can you build a faster fundamental
This works, and I can't imagine how it might cause problems, but visual studio

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.