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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:06:39+00:00 2026-06-12T09:06:39+00:00

Given my variable being a pointer, if I assign it to a variable of

  • 0

Given my variable being a pointer, if I assign it to a variable of “auto” type, do I specify the “*” ?

std::vector<MyClass> *getVector(); //returns populated vector
//...

std::vector<MyClass> *myvector = getVector();  //assume has n items in it
auto newvar1 = myvector;

// vs:
auto *newvar2 = myvector;

//goal is to behave like this assignment:
std::vector<MyClass> *newvar3 = getVector();

I’m a bit confused on how this auto works in c++11 (this is a new feature to c++11, right?)

Update: I revised the above to better clarify how my vector is really populated in a function, and I’m just trying to assign the returned pointer to a variable. Sorry for the confusion

  • 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-12T09:06:40+00:00Added an answer on June 12, 2026 at 9:06 am
    auto newvar1 = myvector;
    
    // vs:
    auto *newvar2 = myvector;
    

    Both of these are the same and will declare a pointer to std::vector<MyClass> (pointing to random location, since myvector is uninitialized in your example and likely contains garbage). So basically you can use any one of them. I would prefer auto var = getVector(), but you may go for auto* var = getVector() if you think it stresses the intent (that var is a pointer) better.

    I must say I never dreamt of similar uncertainity using auto. I thought people would just use auto and not think about it, which is correct 99 % of the time – the need to decorate auto with something only comes with references and cv-qualifiers.

    However, there is slight difference between the two when modifies slightly:

    auto newvar1 = myvector, newvar2 = something;
    

    In this case, newvar2 will be a pointer (and something must be too).

    auto *newvar1 = myvector, newvar2 = something;
    

    Here, newvar2 is the pointee type, eg. std::vector<MyClass>, and the initializer must be adequate.

    In general, if the initializer is not a braced initializer list, the compiler processes auto like this:

    1. It produces an artificial function template declaration with one argument of the exact form of the declarator, with auto replaced by the template parameter. So for auto* x = ..., it uses

      template <class T> void foo(T*);
      
    2. It tries to resolve the call foo(initializer), and looks what gets deduced for T. This gets substituted back in place of auto.

    3. If there are more declarators in a single declarations, this is done for all of them. The deduced T must be the same for all of them…

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

Sidebar

Related Questions

Given <xsl:variable name=datePrecision as=element()*> <p>Year</p> <p>Month</p> <p>Day</p> <p>Time</p> <p>Timestamp</p> </xsl:variable> The expression $datePrecision[5] returns
Given private final Vector v = new Vector(); //instance variable the following 3 lines
I always thought operator is determined if the given variable is of the given
Given this variable in tcsh: set i = ~/foo/bar.c how can I get just
Given a variable which holds a string is there a quick way to cast
Given that I have a variable $peopleSize in the format (I've already extracted the
I need to check a variable vi_theIndex for its value. At the given moment
The compiler, given the following code, tells me Use of unassigned local variable 'x'.
Is there a way to declare a vaiable for an open generic type? Given:
Given the following table variable, which contains let's say some 10K records: DECLARE @SomeTable

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.