0

I have considered an object 'genObj'. This object can be of the class phones, laptops, shoes, or books. I wish to take user input varying from 1-4, and accordingly assign genObj to a praticular class.

I wish to do this because I have to add these objects to data files. I am maintaining 4 separate data files for the different classes, and a value to fileName is also assigned accordingly.

 switch(mode)
    {
     case 1:
        phones genObj;
        strcpy(fileName,"phonesDatabase.dat");
        break;
     case 2:
        computers genObj;
        strcpy(fileName,"computersDatabase.dat");
        break;
     case 3:
        shoes genObj;
        strcpy(fileName,"shoesDatabase.dat)";
        break;
     case 4:
        books genObj;
        strcpy(fileName,"booksDatabase.dat");
        break;
    }

Doing this, hovewer, gives the following error:

"Multiple declaration for genObj in function add(int)"

The values are assigned to fileName without problem as I have declared

char fileName[20];

globally, and it gets initialized later. However, since the object genObj can be of different classes, it cannot be initialized like this.

If I am not able to assign genObj different classes according to user input, I will have to separate the code into 4 parts, each for one class. and that would be too cumbersome, and inelegant.

Thanks.

2
  • If you are writing in C++ (based on your tags) use C++! Remove strcpy from your vocabulary. Commented Nov 4, 2015 at 17:53
  • And the design pattern you are looking for is called a Factory. Google it, please. Commented Nov 4, 2015 at 17:53

2 Answers 2

1

You cannot decide the type of an object dynamically at run-time.

Seems like you should use polymorphism in this situation:

First define an abstract class, say object, that defines the behavior that you use to access your genObj. That is, everything phones, computers, shoes, and books have in commons.

Then make those four sub-classes of object and then something like this:

std::unique_ptr<object> genObj;

switch(mode)
{
    case 1:
        genObj = std::make_unique<phones>();
        strcpy(fileName, "phonesDatabase.dat");
        break;
    case 2:
        genObj = std::make_unique<computers>();
        strcpy(fileName, "computersDatabase.dat");
        break;
    case 3:
        genObj = std::make_unique<shoes>();
        strcpy(fileName, "shoesDatabase.dat)";
        break;
    case 4:
        genObj = std::make_unique<books>();
        strcpy(fileName, "booksDatabase.dat");
        break;
}

(Note that make_unique is a feature only in the most recent C++ standard, so if you're writing your code for an old standard then you will have to use new instead, e.g. genObj.reset(new books).)

Also as a side note, make mode of an enum type, so that you don't have to handle magic numbers and can replace 1, 2, 3, and 4 with some descriptive names such as mode::phones, mode::computers, mode::shoes, and mode::books respectively.

Sign up to request clarification or add additional context in comments.

3 Comments

You should add that this will only work in C++14 (because of make_unique<>). And since you're at it, he may as well use enum classes instead of just enums.
@Blito Added a note about that. And aren't scoped enums enums as well? I didn't dismiss the option of scoped enums, in fact I even hinted at it by using the mode:: prefix.
Yes, they are. The difference is that by using just enums he could also use plain integers instead of mode::phones, etc. The mode:: prefix can be used in (old) enums too, so it's ambiguous. It's just a detail.
0

switch construction won't handle dynamic allocation of objects for you. It'll be much better to employ a fabric here with common interface or abstract class (depending on your needs), say class IFace, and then have a local variable IFace *genObj and then make it equal to new some_derived_from_IFace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.