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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:36:29+00:00 2026-06-13T01:36:29+00:00

I have an implementation of a template class Triple, which is a container holding

  • 0

I have an implementation of a template class Triple, which is a container holding any three types. My problem is that, my class takes three const references to values as parameter, and the values have to be private (definition), however, I also have to implement the copy-constructor and overloaded assignment operator.

template <typename T1, typename T2, typename T3>
    class Triple
{
public:
    Triple()
    { }
    Triple(const T1 &a, const T2 &b, const T3 &c) : a(a), b(b), c(c)
    { }

    // copy constructor
    Triple(const Triple &triple) {
        a = triple.first();
        b = triple.second();
        c = triple.third();
    }

    // assignment operator
    Triple &operator=(const Triple& other) {
        //Check for self-assignment
        if (this == &other)
            return *this;

        a = other.first();
        b = other.second();
        c = other.third();

        return *this;
    }

  private:
    T1 const& a;
    T2 const& b;
    T3 const& c;
 };

How would you implement the copy-constructor and assignment operator without assigning to const variables?

  • 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-13T01:36:30+00:00Added an answer on June 13, 2026 at 1:36 am

    You should probably not have const references as members since you can’t (in general) know that the objects lifetime will outlast the lifetime of your object, a, b and c should almost certainly be of type Tx and not Tx const&.

    If you do know this (be sure that you do, it’s more probable that you don’t understand the implications unless you’re an expert C++ developer), then you can have a copy constructor using an initialization list.

    Triple(const Triple& other) {
      : a(other.a)
      , b(other.b)
      , c(other.c)
    { }
    

    You can’t have assignment operator since assigning to a reference changes the referred to object not the reference, you could simulate references with pointers but since I think this is not what you want I won’t spell it out.

    In any case the real thing you should be doing is using std::tuple and not reinventing The wheel.

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

Sidebar

Related Questions

I have a class that does an implementation of a graph (template class) in
I have a template class that I have made called hash . My template
I have a class: template<typename T> class Foo { public: ... Foo& operator=(const Foo&)
I have an auto pointer implementation: template <typename T, bool Arr = false> class
I have a template class that is only valid for couple of template parameters:
I have a question related to a template class implementation design. A template AT
I have template class ItemContainer that actually is facade for a whole family of
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
For example: I have an implementation (say doSomething()) in Base class. I want all
I know that it's OK for a pure virtual function to have an implementation.

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.