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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:06:19+00:00 2026-05-15T20:06:19+00:00

I have got the following code #include <iostream> #include <string> template <typename T> class

  • 0

I have got the following code

#include <iostream>
#include <string>

template <typename T>

class demo
{
    T data;
  public:
    demo();
    demo(demo const&k );
    demo(const T&k);
    demo& operator=(const demo &k);

    template<typename T1>
    demo(const demo<T1>&k);

    template<typename T1>
    demo<T>& operator=(const demo<T1>&k);

   ~demo();
};

template <typename T>
demo<T>::demo():data(){}

template<typename T>
demo<T>::demo(demo const& k):data(k.data){}

template<typename T>
demo<T>::demo(const T&k):data(k){}

template<typename T>
demo<T>& demo<T>::operator=(demo const &k)
{
    if ((void*)this == (void*)&k) {    // assignment to itself? 
         return *this; 
        } 
    this->data=k.data;
    return *this;
}

template<typename T>
  template<typename T1>
  demo<T>& demo<T>::operator=(demo<T1> const &k)
  {
    if ((void*)this == (void*)&k) {    // assignment to itself? 
         return *this; 
        } 

    this->data=k.data;
    return *this;
  }

template<typename T>
  template<typename T1>
  demo<T>::demo(const demo<T1>&k):data(k.data){}

template<typename T>
demo<T>::~demo(){}

int main()
{
    demo<std::string> k(std::string("hello"));
    demo<std::string >l=k;
    demo<int> x(10);
    demo<double> p=x; //error here
}

Why do I get error here? As far as I know p is being copy initialized to x. So

 demo<T>::demo(const demo<T1>&k):data(k.data){} 

is invoked. But since data is a private member I get an error 'demo<T>::data' : cannot access private member declared in class 'demo<T>'. Why?

I know that member functions of a class can access private members , so why do I get an error? What should I do to correct the error?

  • 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-15T20:06:20+00:00Added an answer on May 15, 2026 at 8:06 pm

    Because demo<T> and demo<T1> are considered different types they may not access each other’s private data.

    Easist way to solve this is by adding a public accessor function and using it:

    template <typename T>
    class demo
    {
    public:
        const T &get_data() const { return data; }
        ...
    };
    
    template<typename T>
    template<typename T1>
    demo<T>::demo(const demo<T1>&k):data(k.get_data()){}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 470k
  • Answers 470k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When you use $var = new Class a new object… May 16, 2026 at 3:10 am
  • Editorial Team
    Editorial Team added an answer You can make a plain view (or UILabel with the… May 16, 2026 at 3:10 am
  • Editorial Team
    Editorial Team added an answer I solved the problem. Here I leave you the code,… May 16, 2026 at 3:10 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.