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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:00:52+00:00 2026-05-18T21:00:52+00:00

assume the following class is given: class Base{ public: Base() {} Base( const Base&

  • 0

assume the following class is given:

class Base{
public:

Base() {}
Base( const Base& b) : base_attr(b.base_attr) {}

void someBaseFunction()
{ .... }

protected:

SomeType base_attr;
};

When I want a class to inherit from this one and include a new attribute for the derived class, I would write:

class Derived: public Base {
public:

Derived() {}
Derived( const Derived& d ) : derived_attr(d.derived_attr)
{
  this->base_attr = d.base_attr;
}

void SomeDerivedFunction()
{ .... }

private:

SomeOtherType derived_attr;
};

This works for me (let’s ignore eventually missing semicolons or such please).

However, when I remove the “this->” in the copy constructor of the derived class, the compiler complains that “‘base_attr’ was not declared in this scope”.

I thought that, when inheriting from a class, the protected attributes would then also be accessible directly. I did not know that the “this->” pointer was needed.
I am now confused if it is actually correct what I am doing there, especially the copy-constructor of the Derived-class.

Because each Derived object is supposed to have a base_attr and a derived_attr and they obviously need to be initialized/set correctly.
And because Derived is inheriting from Base, I don’t want to explicitly include an attribute named “base_attr” in the Derived-class. IMHO doing so would generally destroy the idea behind inheritance, as everything would have to be defined again.


EDIT

Thank you all for the quick answers. I completely forgot the fact that the classes actually are templates.

Please, see the new examples below, which are actually compiling when including “this->” and are failing when omiting “this->” in the copy-constructor of the Derived-class:
Base-class:

#include <iostream>

template<class T> 
class Base{
public:

Base() : base_attr(0) {} 
Base( const Base& b) : base_attr(b.base_attr) {} 

void baseIncrement()
{ ++base_attr; }

void printAttr()
{
        std::cout << "Base Attribute: " << base_attr << std::endl;
}

protected:

 T base_attr;
};

Derived-class:

#include  "base.hpp"

template< class T >
class Derived: public Base<T>{
public:

Derived() : derived_attr(1) {} 
Derived( const Derived& d) : derived_attr(d.derived_attr)  {  
        this->base_attr = d.base_attr;
        }  

void derivedIncrement()
{ ++derived_attr; }

protected:

 T derived_attr;
};

and for completeness also the main function:

#include "derived.hpp"

int main()
{
        Derived<int> d; 
        d.printAttr();

        d.baseIncrement();
        d.printAttr();

        Derived<int> d2(d);
        d2.printAttr();

        return 0; 
};

I am using g++-4.3.4.
Although I understood now that it seems to come from the fact that I use template-class definitions, I did not quite understand what is causing the problem when using templates and why it works when not using templates. Could someone please further clarify this?

  • 1 1 Answer
  • 1 View
  • 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-18T21:00:52+00:00Added an answer on May 18, 2026 at 9:00 pm

    You are only seeing this if Base in some way depends on template arguments.

    In that case, it’s deemed too dangerous that a name like base_attr is lookup up in such a dependent base class: For some template instantiation, the name could be found in the base class, and for another instantiation, the member could be absent and the name would refer to a some namespace member.

    Because this was thought to be confusing, C++ follows the consistent rule that base_attr is never lookup in a dependent base class when doing unqualified name lookup. You need to prefix the name with this-> or with the name of the class as in Derived<T>::base_attr. So when the base class doesn’t declare a base_attr, the name doesn’t silently refer to a potentially globally declared name, but it will just be a compile time error.

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

Sidebar

Related Questions

Let's assume I have the following code: public class MainClass { public static void
I have the following question: Assume base class A with method: A& operator+(A& a)
Assume the following class public class TestObject{ public void synchronized method1(){ //some 1000 lines
Given the following block of code: class BaseClass { public: virtual void hello() {
Given the following code : import java.io.*; public class Main { public static void
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public
Lets assume we have the following code: abstract class Base1 { protected int num;
Assume we have the following, given code: class T; // with T::~T is virtual
I have a generic method in a base class similar to the following: protected

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.