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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:27:36+00:00 2026-05-30T08:27:36+00:00

The default copy constructor that is synthesized by the compiler for a class that

  • 0

The default copy constructor that is synthesized by the compiler for
a class that does not define its own does the right thing: it copies
all the members from one object to another.

I want to do something similar. I want to write a method

template <typename T>
T f(const T& obj) {
 // for each member var i of obj, I want to call some method g(obj.i)
}

Now I don’t know what the names of the member variables are. If this was
the copy constructor, I could call the assignment operator instead of g.

Clearly the compiler does this (but maybe it does this after it has inferred
the name of the members of the class). Is it even possible to do this
for any class T ?

  • 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-30T08:27:37+00:00Added an answer on May 30, 2026 at 8:27 am

    The compiler has some internal data structure representing each class. When it comes to synthesize the copy constructor, it can refer to this structure in order to figure out what code it needs to emit (how many copies, how each one is done, and what the addresses of the members are in relation to the source and destination object addresses).

    As a mere C++ programmer, you don’t have access to this internal compile-time data structure, so you’re pretty much out of luck. You basically have to list the members and hope you don’t leave any out.

    You could perhaps do work with the preprocessor (or if not the preprocessor then a preprocessor), to annotate your struct definition with extra information that you can use to generate the list of one call for each member.

    • 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 requires a non-default copy constructor and assignment operator (it
I know that the compiler sometimes provides a default copy constructor if you don't
I wanted to know Why compiler provides default copy constructor..Whats the strategy behind that
[C++11: 12.8/7]: If the class definition does not explicitly declare a copy constructor, one
I need to write a copy constructor that deep copies the contents of a
Take a simple class with the big 3 (constructor, copy constructor, destructor): #include <vector>
I have a class (Uniform) that has a constructor with 2 parameters, and a
I have a not-so-small class under development (that it changes often) and I need
The code I'm working with has its own smart pointer implementation which does simple
I frequently run into the problem, that I must extend a compiler generated copy

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.