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

Related Questions

I have got the following issue, my function appends code to a string $string
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
I have the following C++ code. #include <iostream> using namespace std; int testfunction(int input)
I have the following C++ code which I got from google's sparsehash website: #include
i was dealing with the following code,& got confused,please have a look at it
I have following code to accomplish prefix sum task: #include <iostream> #include<math.h> using namespace
I have got the following very simple code: function init() { var articleTabs =
I have the following code and got myself confused: I have a query that
I have the following code, but i got a problem. Within my While, a
I have got the following class structure, and have plenty of classes like C

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.