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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:57:05+00:00 2026-06-01T20:57:05+00:00

Reference code: #include <vector> #include <iostream> class Func { public: virtual void call() {

  • 0

Reference code:

#include <vector>
#include <iostream>

class Func {
public:
    virtual void call() {
        std::cout<< "Func -> call()" << std::endl;
    }
};

class Foo : public Func {
public:
    void call() {
        std::cout<< "Foo -> call()" << std::endl;
    }
};

class Bar : public Func {
public:
    void call() {
        std::cout<< "Bar -> call()" << std::endl;
    }
};

int main(int argc, char** argv) {
    std::vector<Func> functors;

    functors.push_back( Func() );
    functors.push_back( Foo() );
    functors.push_back( Bar() );

    std::vector<Func>::iterator iter;
    for (iter = functors.begin(); iter != functors.end(); ++iter)
        (*iter).call();
}

When run that code, it produces the following output on my computer:

$ ./test
Func -> call()
Func -> call()
Func -> call()

Would there be any way to ensure that the correct virtual function is called in this instance? I’m new at C++ but my best guess is that here:

(*iter).call();

It’s being cast to a Func object. Is this correct?

  • 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-01T20:57:06+00:00Added an answer on June 1, 2026 at 8:57 pm

    You should use a shared_ptr or unique_ptr to hold the elements of a collection of polymorphic type.

    As your code is written now the Foo and Bar instances are cooerced (copy constructed) into an instance of type Func to fit into the vector. (The reason is that vector stores its elements immediately by fixed-sized value for performance, however polymorphic subclasses are of a arbitrarily larger size unknown at compile-time, so it can only store the base class.)

    This is better:

    int main(int argc, char** argv) {
        vector<shared_ptr<Func>> functors;
    
        functors.push_back( make_shared<Func>() );
        functors.push_back( make_shared<Foo>() );
        functors.push_back( make_shared<Bar>() );
    
        for (auto functor : functors)
            functor->call();
    }
    

    In the above a reference-counted pointer is used to implicitly share the hetrogeneous subclasses of Func in the vector. (This indirection allows arbitrarily sized subclasses of Func to be stored by address indirection.)

    Also, you may want to take a look at std::function and std::bind, rather than rolling your own functor type.

    Another thing to look at would be perfect forwarding and varadic templates.

    update: For old compiler:

    int main(int argc, char** argv) {
        vector<std::tr1::shared_ptr<Func> > functors;
    
        functors.push_back( std::tr1::make_shared<Func>() );
        functors.push_back( std::tr1::make_shared<Foo>() );
        functors.push_back( std::tr1::make_shared<Bar>() );
    
        for (size_t i = 0; i < functors.size(); ++i)
            functors[i]->call();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
#include<iostream> #include<vector> std::vector<std::string> vector1; int main() { vector1.push_back(adadad); std::vector<std::string> vector2; vector2.push_back(adadd); if (vector1==vector2) {
I've been looking at C++0x threads and have this code: #include <vector> #include <iostream>
I can't find bug in this code In function `BinaryCode::decode(std::string)': undefined reference to `BinaryCode::m_vecStr'
//C++ Example #include <iostream> using namespace std; int doHello (std::string&); int main() { std::string
The problem I have is illustrated in the following code. #include <iostream> #define X
I'm trying to figure out what this piece of code does: std::vector<std::vector<bool> > visited(rows,
Ho do you reference code inside an AJAX tabcontainer? Is there an easy way?
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating

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.