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 7715961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:31:05+00:00 2026-06-01T02:31:05+00:00

My base class, class Foo , is a template class which has the following

  • 0

My base class, class Foo, is a template class which has the following constructor:

//Declarations
template <class T>
class Foo
{
public:
    Foo::Foo(std::string& root);
    MemberClass obj;
}

template <class T>
Foo<T>::Foo(std::string& root)
{
    MemberClass obj(root); // initialize the member object
    obj.getRoot(); // Prints the string
    // ...
}

It has a child class, which is constructed like so:

template <class T>
Bar<T>::Bar(std::string& root)
    : Foo<T>(root)
{
    //...
}

template<class T>
void
Bar<T>::accessObj()
{
    this->obj.getRoot();
    // Prints the empty string
}

This gives unexpected behaviour, even though no errors are generated. In this case, getRoot() will return the empty string.

I have tested this by altering the Foo constructor like so:

{
    MemberClass obj(root);
    std::cout << &obj << std::endl;
}

and the Bar constructor like so:

//...
    : Foo<T>(root)
{
    std::cout << &this->obj << std::endl;
}

The output gives two different locations in memory, which is totally blowing my mind. Why is this the case? How do I fix it?

  • 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-01T02:31:06+00:00Added an answer on June 1, 2026 at 2:31 am

    Your constructor might better look like this:

    template <class T>
    Foo<T>::Foo(std::string& root)
        : obj(root) // initialize the member object
    {
        obj.getRoot(); // Prints the string
        // ...
    }
    

    Before, you were constructing a temporary obj in your Foo constructor and throwing it away, never initializing your member variable at all.

    If you’re using GCC, the option -Wshadow might have helped catch this mistake.

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

Sidebar

Related Questions

I have a base class Foo that has an Update() function, which I want
Given this code: class base { public: string foo() const; // Want this to
Imagine a base class with many constructors and a virtual method public class Foo
Assuming one has an abstract base class foo with __get() defined, and a child
For example: Base class header file has: enum FOO { FOO_A, FOO_B, FOO_C, FOO_USERSTART
In base class constructors I always see a parameterless constructor, like so: public abstract
I have a base class that looks like the following: template<typename T> class Base
template<typename T> class Base { protected: Base() {} T& get() { return t; }
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I am defining the following class: public class Foo<T> { T _value; public T

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.