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

  • Home
  • SEARCH
  • 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 8268923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:03:27+00:00 2026-06-08T06:03:27+00:00

I have a (virtual) function which returns a std::vector with a single value. Let’s

  • 0

I have a (virtual) function which returns a std::vector with a single value. Let’s assume the class looks like:

#include <vector>

class Z; // irrelevant

class C
{
    Z& something;

public:
    typedef std::vector<Z*> list_type;

    virtual list_type f();
};

Is it better to use the repetitive constructor:

C::list_type C::f()
{
    return list_type(1, &something);
}

or just push_back() the single element:

C::list_type C::f()
{
    list_type ret;

    ret.push_back(&something);

    return ret;
}
  • 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-08T06:03:28+00:00Added an answer on June 8, 2026 at 6:03 am

    If you consider “better” to be “short code”, then probably the repetitive constructor is better because is shorter and therefore more concise.

    In terms of functionality, both are equal.

    In terms of performance, the repetitive constructor can be better because it can call reserve() as it knows beforehand the total number of elements that it’s going to allocate. However, you are using it just to create an element, so I guess it does not matter.

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

Sidebar

Related Questions

I have a class (for logging) which derives from std::ostream. Stripped down, it looks
Here I have declared another virtual function in Derived class. #include <iostream> using namespace
I have an abstract class with a pure virtual function f() and i want
I have a function X() declared as PURE VIRTUAL in base class: class Base
I have a declaration in a cpp where a function is like: virtual void
Let's say I have a base class Animal from which a class Cow inherits,
I have a Network class, which I want two virtual functions which I am
Possible Duplicate: c++ virtual function return type I have a simple but confusing question
I know that it's OK for a pure virtual function to have an implementation.
I have a function with the following definition: virtual bool Process(wtdaFileHandler &daHandler, wtdaGather &daGather);

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.