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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:43:07+00:00 2026-05-21T08:43:07+00:00

First off, I apologize if I’m not using the right terms in my question

  • 0

First off, I apologize if I’m not using the right terms in my question title. It is entirely possible. I am starting to learn c++, and I will admit I’m having a really tough time with references and copies. In reference to my code below, I have the following questions:

  1. In main.c, is the argument passed to the Carrier.add() function (*it), a reference, or a copy of the plan object that I put in the vector, or something else?

  2. When this argument gets added to the Carrier’s vector of Plans (inside the carrier class), is a copy added? How does that work since I didn’t supply a copy constructor or tell it to make a copy? I think it is automatically generated?

  3. Please ignore this one if it is too general. I guess my main problem is that I want/need to understand how it is working, and this is where am having problems trying to find out the answers to my questions on my own. Is there a way to make visual studio generate the constructors in my actual code the same way that the compiler would, so I could step through them in debug mode to see how it all is working. Right now when I debug, it is hard for me to tell that a compiler-generated copy constructor is called (if that is what is happening at all).

Here is my code:

main.c

#include "stdafx.h"
#include <iostream>
#include "Plan.h"
#include "Carrier.h"

int _tmain(int argc, _TCHAR* argv[])
{

    std::vector<Plan> plans;

    for (int i = 0; i < 4; ++i) {
        Plan p(i);
        plans.push_back(p);
    }

    Carrier c(5);
    for(std::vector<Plan>::iterator it = plans.begin(); it != plans.end(); ++it) {
            //my question is about the line directly below this comment
        c.add(*it);
    }
    return 0;
}

Plan.h

#pragma once

class Plan
{
private:
    int id;
public:
    Plan(int id);
};

Plan.cpp

#include "Plan.h"

Plan::Plan(int i)
{
    id = i;
}

Carrier.h

#pragma once
#include <vector>
class Plan;
class Carrier
{
private:
    int id;
    std::vector<Plan> plans;
public:
    Carrier(int i);
    void add(Plan p);
};

Carrier.cpp

#include "Carrier.h"
#include "Plan.h"

Carrier::Carrier(int i) {
    id = i;
}

void Carrier::add(Plan p) {
    plans.push_back(p);
}
  • 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-21T08:43:08+00:00Added an answer on May 21, 2026 at 8:43 am

    (1) Since Carrier::add does not take its argument by reference, it is passed a copy. To have it take a reference, change its signature to

    void Carrier::add(Plan const &p);
    

    (2) When you do push_back, a copy is created regardless of whether you pass a Plan by reference or by value/copy; that’s part of the semantics of std::vector (and all the other standard containers). The copy constructor is generated automatically by the compiler.

    (3) I’m not a VC++ user, but the copy constructor that the compiler will generate is equivalent to

    Plan::Plan(Plan const &other)
     : id(other.id)  // there's only one member,
                     // but other members would be treated the same
    {}
    

    In the empty body, you could add a printing statement indicating that the copy constructor is called, but that would not be waterproof; in some cases, C++ is allowed to perform the copy elision optimization, meaning that the actual copying is skipped.

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

Sidebar

Related Questions

No related questions found

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.