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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:34:08+00:00 2026-06-05T17:34:08+00:00

Apple.h class Apple { public: Apple(int); static int typeID; private: int id_; }; Apple.cpp

  • 0

Apple.h

class Apple {
public:
    Apple(int);
    static int typeID;
private:
    int id_;
};

Apple.cpp

#include "Apple.h"
Apple::Apple(int pID) {
    id_ = pID;
}

Potato.h, Potato.cpp identical to Apple

Storage.h

#pragma once
#include "Apple.h"
#include "Potato.h"
#include <vector>
class Storage {
public:
    Storage();
    template<typename foodName> void store(foodName * object){
        (*getBasket<foodName>()).push_back(object);
    };
    template<typename foodName> int countSize(){
        return (*getBasket<foodName>()).size();
    };

private:
    std::vector<Apple*> applebasket_;
    std::vector<Potato*> potatobasket_;
    template <typename foodName> std::vector<foodName*> * getBasket(){
        std::vector<foodName*> * result;
        switch(foodName::typeID){
            case 0:
                result = &applebasket_;
                break;
            case 1:
                //result = &potatobasket_;
                break;
        }
        return result;
    } 
};

Storage.cpp

#include "Storage.h"
int Apple::typeID;
int Potato::typeID;
Storage::Storage() {
    Apple::typeID = 0;
    Potato::typeID =1;
}

main.cpp

#include "Storage.h"
#include <iostream>
int main() {
    Apple* apple;
    Potato* potato;
    Storage storage;
    int i;
    for(i = 0;i < 7;i++){
        apple = new Apple(i);
        storage.store<Apple>(apple);  
    }      
    std::cout<<storage.countSize<Apple>();
    return 0;
}

This code works and outputs right size of vector, but if case line in switch statement (inside Storage.h) is uncommented, compiler(g++) throws
“error: cannot convert ‘std::vector < Potato*>* ‘ to ‘std::vector< Apple*>* ‘ in assignment”.
It is like compiler trying out both cases anyway, and i cant find is it possible and how to avoid this.
I need help with this and maybe some advice on a whole thing(one interface for containers of different types), I’ve started learning C++ recently and probably the way i try this to do here is a total mess.

  • 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-05T17:34:09+00:00Added an answer on June 5, 2026 at 5:34 pm

    Your code doesn’t compile because both case should compile which is not possible, as the type differs in both case.

    One solution to this problem is to use overload instead of function template as (which means, there is NO need of typeID in your class!) :

    std::vector<Apple*> * get_basket(Apple *)
    {
       return &applebasket_;  //return pointer to the apple basket
    }
    
    std::vector<Potato*> * get_basket(Potato *)
    {
       return &potatobasket_; //return pointer to the potate basket
    }
    

    And call it as:

    template<typename FoodType> 
    void store(FoodType * object)
    {
        std::vector<FoodType> * basket = get_basket(static_cast<FoodType*>(0));
        basket->push_back(object);
    }
    

    The trick here is that you have two overloads, each takes one argument of different type, and so you use static_cast<FoodType*>(0) to help the compiler to pick the correct overload based on the type of the expression static_cast<FoodType*>(0) which would be either of type Apple* or Potato*.


    @Gorpik said in the comment that both (this as well as other solution) are ugly, so here is another attempt to solve this problem.

    Define a base_storage class template as:

    template<typename FoodType>
    class base_storage
    {
        std::vector<FoodType*> m_storage;
        public:
            void store(FoodType *foodItem)
            {
                m_storage.push_back(foodItem);
            }
            size_t count() const
            {
                return m_storage.size();
            }
    };
    

    This base class stores food items of one type only, but in the question, we need to store food items of two types. So in order to do that, lets define another class Storage deriving from the above class template as:

    class storage : private base_storage<Apple>, private base_storage<Potato>
    {
        public:
            template<typename FoodType> 
            void store(FoodType * foodItem)
            {
                base_storage<FoodType>::store(foodItem);
            }
            template<typename FoodType> 
            size_t count() const
            {
                return base_storage<FoodType>::count();
            }
    };
    

    Note two points here:

    • The class storage doesn’t have any member data now. It just forward the call to the base class which is chosen based on the type of template argument FoodType.
    • It derives privately from the base classes. So it is not is-a relationship.

    See the online demo of this solution here : http://ideone.com/Ykjo5

    The beauty of this solution is that if you want to make it work for three types of food, then all you need to derive it from three base classes as:

    class storage : private base_storage<Apple>, 
                    private base_storage<Potato>,
                    private base_storage<Mango>   //added line!
    {
    
         //same as before; no change at all !
    
    };
    

    Demo : http://ideone.com/lnMds

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

Sidebar

Related Questions

class background { public static void moon(Graphics g) { int k2 = 0; int
class apple { public : operator orange () const { cout << operator; }
This is my code: public class demoProject1 extends Activity { int i; /** Called
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I have this getView method inside my ListViewAdapter: public static class ViewHolder{ public TextView
public class recursionExcercise4 { public static void main (String[] args) { boolean statement=false; String
I am making some game and I get following error : class Apple:public Fruit{
Function abstraction: public abstract class Function<X, Y> { abstract Y apply(X x); } max
Apple Mail defines both a class account and a constant account for the rule
In Apple's The Objective-C Programming Language: Defining a Class the section named Redefining self

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.