ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have to write a program that uses 3 C++ classes. The user knows only about one of them (let's say class C). The C constructor, depending on the input received should decide with what type of object to work with. Any ideas? Thanks.
since you say C is exposed, and 'uses' the other classes, does that mean you want C to contain an instance of either A or B, and it will choose which one based on the C constructor?
i'm not sure if this is the most elegant way to do it, but could you have a member variable in C called useA, and initialize it accordingly in the C constructor.
and then in every C member that would use either class A or B, you'd go
Ok just to make sure what you are asking, as you said "exactly" to something that isn't polymorphism. Are A and B types of C?
If they are and the user does not know about A and B then the correct way would be to use the design pattern factory.
i'm not sure if this is the most elegant way to do it, but could you have a member variable in C called useA, and initialize it accordingly in the C constructor.
and then in every C member that would use either class A or B, you'd go
if (useA) {A related code} else {B related code}
You're right John it's not very elegant.
Quote:
Originally Posted by johnty01
Ok just to make sure what you are asking, as you said "exactly" to something that isn't polymorphism. Are A and B types of C?
If they are and the user does not know about A and B then the correct way would be to use the design pattern factory.
Please elaborate dmail. I thought I'd use polymorphism. A and B don't have to be types of C. The design is up to me. Thank you both for for ideas.
No, I have to use 3 classes: one to call one of the other 2; it's a school assignment.
Why didn't you say this in the first place
You will have to think about it yourself then or consult your tutor. You will not learn by somebody supplying the answer for you.
I didn't ask for the code only the idea. What's the point of this forum if not to ask questions and it's not like you've told me much. Thanks anyway for your time.
I didn't ask for the code only the idea. What's the point of this forum if not to ask questions and it's not like you've told me much. Thanks anyway for your time.
did you happen to review the Factory pattern that dmail mentioned. that may shed some light on it for you, and then you could ask questions that make a little more sense.
I agree that you should ask your instructor... after you have thoroughly reviewed the material to see if you can discover the answer for yourself.
Instructors need feedback! (Believe me, I know...) They can be at the front of that classroom thinking that they are absolutely in a vacuum. When one student comes back with a question, they know that many other students must have the same problem; without it, they don't.
Perhaps you do not yet understand the problem; to me, your description of it was pretty vague. If you don't understand what is being asked of you, that might make it impossible to arrive at the answer. And if that be the case, I can flatly say to you that you are not the only one. "It happens. A lot." Believe me, it's okay to (timely...) ask the instructor to clarify. I assure you that there have been many times when I have sent a "broadcast message to all students" in response to a single inquiry from any one.
That instructor is, after all, in a way working for you. You're paying good money to learn this stuff. It's a two way street. The point, after all, is not that the instructor knows the material! The point is that, by the end of the course, you do, and (of course) you've got the good grades to prove it. If information is flowing in one direction only, it ain't gonna happen and you're wasting your time.
Last edited by sundialsvcs; 01-13-2007 at 08:31 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.