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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:33:33+00:00 2026-05-22T18:33:33+00:00

I figured out that it’s possible to initialize the member variables with a constructor

  • 0

I figured out that it’s possible to initialize the member variables with a constructor argument of the same name as show in the example below.

#include <cstdio>
#include <vector>

class Blah {
    std::vector<int> vec;

public:
    Blah(std::vector<int> vec): vec(vec)
    {}

    void printVec() {

        for(unsigned int i=0; i<vec.size(); i++)
            printf("%i ", vec.at(i));

        printf("\n");
    }
};

int main() {

    std::vector<int> myVector(3);

    myVector.at(0) = 1;
    myVector.at(1) = 2;
    myVector.at(2) = 3;

    Blah blah(myVector);

    blah.printVec();

    return 0;
}

g++ 4.4 with the arguments -Wall -Wextra -pedantic gives no warning and works correctly. It also works with clang++. I wonder what the C++ standard says about it? Is it legal and guaranteed to always work?

  • 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-22T18:33:34+00:00Added an answer on May 22, 2026 at 6:33 pm

    I wonder what the C++ standard says about it? Is it legal and guaranteed to always work?

    Yes. That is perfectly legal. Fully Standard conformant.

    Blah(std::vector<int> vec): vec(vec){}
                                 ^   ^                           
                                 |   |
                                 |    this is the argument to the constructor
                                 this is your member data
    

    Since you asked for the reference in the Standard, here it is, with an example.

    §12.6.2/7

    Names in the expression-list of a mem-initializer are evaluated in the scope of the constructor for which the mem-initializer is specified.

    [Example:
    class X {
     int a;
     int b;
     int i;
     int j;
     public:
     const int& r;
      X(int i): r(a), b(i), i(i), j(this->i) {}
                          //^^^^ note this (added by Nawaz)
    };
    

    initializes X::r to refer to X::a,
    initializes X::b with the value of the
    constructor parameter i, initializes
    X::i with the value of the constructor
    parameter i, and initializes X::j with
    the value of X::i; this takes place
    each time an object of class X is
    created. ]

    [Note: because the
    mem-initializer are evaluated in the
    scope of the constructor, the this
    pointer can be used in the
    expression-list of a mem-initializer
    to refer to the object being
    initialized. ]

    As you can see, there’re other interesting thing to note in the above example, and the commentary from the Standard itself.


    BTW, as side note, why don’t you accept the parameter as const reference:

     Blah(const std::vector<int> & vec): vec(vec) {}
          ^^^^const              ^reference
    

    It avoids unneccessary copy of the original vector object.

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

Sidebar

Related Questions

So I figured out that by adding the ResetWebServer=FALSE attribute to the solution manifest
I figured out that CUDA does not work in 64bit mode on my mac
I have figured out that I can use hibernate to get the sum of
I have figured out that to integrate calls between Apache HTTP server and Restlet,
I just figured out that I have a problem in IE while working with
So I figured out that the replace function, that finds all single-letter words (prepositions
By a question that I made, I figured out that tho copy elements from
I created an UIViewController subclass, and figured out that the default implementation of -loadView
I figured out that it is impossible to use a custom facebook login since
So far I figured out that one can prevent Eclipse from complaining about proprietary

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.