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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:37:14+00:00 2026-06-05T20:37:14+00:00

#include <list> using std::list; class foo { … class bar : public foo {

  • 0
#include <list>
using std::list;
class foo { ...
class bar : public foo { ...
static void print_all(list<foo*> &L) { ...
list<foo*> LF;
list<bar*> LB;
...
print_all(LF); // works fine
print_all(LB); // static semantic error

I think i know why the compiler will not allow the second call. Can anyone give an example of bad things that could happen if the compiler accepts this kind of call?

  • 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-05T20:37:16+00:00Added an answer on June 5, 2026 at 8:37 pm

    There are two different things going on in the code, with different explanations. The first problem is that different instantiations of a template are unrelated, even if the instantiating types are related. In this particular case std::list<foo*> has no relationship with std::list<bar*>, even if foo is a base of bar. This is part of the design of the language and nothing can be done.

    The second problem, which is not what the compiler is complaining about is that in general a container of derived cannot be used by reference as a container of base. That is the issue that @templatetypedef brought up –but again, this is not the issue in the code, it would be in a different example:

    void f( base** p );
    int main() {
       derived *d;
       f( &d );          // error
    }
    

    In this case, the issue is that, as @templatetypedef points out that using a pointer/container of derived in place of a pointer/container to base in a non-const manner is prone to errors, as you could store non-derived types on the pointer/container.

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

Sidebar

Related Questions

following linked list implementation with stacks works fine #include <iostream> using namespace std; class
#include <list> #include <set> #include <iterator> #include <algorithm> using namespace std; class MyContainer {
Here is simple linked list code: #include <iostream> using namespace std; class link {
#ifndef SLIST_H #define SLIST_H #include llist.h using namespace std; class slist:public llist{ public: slist();
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
here is my code for xor linked list implementation #include<iostream> using namespace std; struct
#include<iostream> #include<time.h> #include<list> #include<stdlib.h> #include<fstream> using namespace std; typedef struct diskBtNode { int parent;
I have the following code to test memory deallocation using a std::list container: #include
When compiling this code on VS2008: #include <vector> using namespace std; class Vertex {
I have the following code : #include <cstdarg> #include <iostream> using namespace std; class

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.