Why object creator method of factory pattern is accepting string parmeter? - factory-pattern

Why object creator method of factory pattern is accepting string parmeter?
Why generic creator method is not common?

Related

E0322: object of abstract class type "" is not allowed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have a pure virtual function in my header file and a static class instance:
static Class mInstance;
I get the following error:
object of abstract class type "Class" is not allowed: function "" is a
pure virtual function.
What does this mean and how can I fix this?
Some more details:
My wish is to use the mInstance variable in other non-derived classes. The pure virtual function is being overriden by derived classes. Can I ask what kind of details I should provide? The mInstance variable is being used with a FactoryMethod that I wrote.
When you create an abstract class you are telling the compiler that there is no reason to create an instance of the class itself. This is usually done when you define an interface - set of methods and or fields for a base class that should work for various inherited classes but not base itself. So compiler helps you to avoid unintentional mistakes and does not allow to create an instance. So there are 2 possible solutions:
you made your class abstract by mistake and it should not be. Solution is simple just make all virtual functions not pure and implement them
your class should be abstract indeed. Then you should not have instance of the class, static or not. Usually when you have interface you work with pointer or reference to the base class and assign them to derived class that implements the interface. What should be done in your case is not clear as we do not have enough information, simplest in your case to make that static variable a pointer (probably a smart one) and assign it to an instance to derived class somewhere.

Calling a method with no name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this line that I need to be able to run with the code I am writing:
euro(2, Regular)
euro is an object from a class named MainControl, and I have an enum defined:
enum VoterType { All, Regular, Judge };
Basically I understand that we want to use some method from the class named MainControl, but I don't see the name of the method that should be used?
Given the fact that this function returns void, how exactly is the declaration supposed to look like in the class MainControl?
MainControl class is implementing a custom overloaded operator, i.e., "function call operator".
How exactly is the declaration supposed to look like in the class MainControl?
The method signature should be something similar to:
class MainControl {
// ...
public:
void operator()(int, VoterType);
};
When a user-defined class overloads the function call operator, operator(), it becomes a FunctionObject type.
In short, your class instance (euro) is a FunctionObject and that particular method can be simply invoked as (for example):
euro(2, VoterType::Regular)
Additional Notes
Just for sake of completeness, the "operator-method" is just like another method (no black magic here).
Indeed, you can even invoke it with the complete name:
euro.operator()(2, VoterType::Regular);
But the syntax becomes pretty ugly.

Using static method in Factory: Pros and Cons? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
In C++, when you have a Factory class for an object, the method that creates the object may be used as one of the following:
non-static method:
Declaration:
class FooFactory{
public:
Foo* Create() {
return new Foo();
}
};
Usage
FooFactory fooFactory;
Foo* foo = fooFactory.Create();
static method:
Declaration:
class FooFactory{
public:
static Foo* Create() {
return new Foo();
}
};
Usage
Foo* foo = FooFactory::Create();
Using the static method avoids instantiating the factory, for instance.
Is this a good practice or are there any other good arguments in favor or against both solutions?
I'd appreciate a list of pros and cons that could help me and the community to choose which to use.
People should refrain from using opinions as I'm looking for feedback based on expert experience.
The main reason to choose one over the other is whether there is additional information that will change the instantiation. If the factory class has such information (and that information can vary rather than simply being set) then it makes sense for the factory method to be non-static. If the factory will simply create the object (or return the address of a function local static instance) there is no reason to require ath factory type be instantiated.
An example of when it would make sense for the factory method to be non-static would be a factory for database connection objects, you instantiate the factory, set whatever properties for the database connection you want then invoke CreateConnection() to get a connection object with the specified properties. This can be Superior to overloading the CreateConnection method with different parameter options because the properties can vary so much from one databse connection to another.
An example where it would make sense for the method to be static would be a method that returns an instance of the actual factory just described. That factory type is simply created, there is no tuning involved.
Agree with the previous answer. Couple more things.
std::unique_ptr is usually better than raw pointers.
Another one, if the only thing your class factory does is that call to new Foo(); you don’t need a class factory at all. FooFactory::Create(); is not longer than std::make_unique<Foo>(). Class factories are only useful when you’re de-serializing stuff, or when you’re constructing different classes based on arguments or some configs.

Not using dynamic_cast [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
There is an abstract class BasePet and some child classes. I want to make a function
BasePet* foo(BasePet*);
which will make copies of objects from static memory to heap. Is there any way except using dynamic_cast? (For example, if there may be another child classes which are unknown for me and I want foo() to be usable for them all).
If you can modify BasePet and its derived classes, the typical way is to create a clone method in BasePet and require that each derived class override it. Generally this looks like:
/*
This is what BasePet::clone() would look like if you wrote it, but normally that would be pure virtual.
BasePet *BasePet::clone() const
{
return new BasePet(*this);
}
*/
BasePet *Dog::clone() const
{
return new Dog(*this);
}
BasePet *Cat::clone() const
{
return new Cat(*this);
}
// ... and so on ...
If you don't have access to the class implementations, my first recommendation would be to try and figure out a way to do what you're trying to do without copying the objects, due to the concerns you raised about new derived classes appearing, as well as the implementations of the classes changing in ways that might affect what the code needs to do. If there's absolutely no way around it, you'll need to use some form of run-time type identification (RTTI); I would use typeid rather than dynamic_cast so that you can test for an exact match and don't get tripped up if the derived class D Is itself the base class for another derived class D2 that you didn't know about.

C++ Derived class constructors [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Say i have a class representing the subscribers for a library. I then want to create multiple other derived classes representing different types of subscribers such as students, professors etc..
How do i write the derived class constructor ? In my base class Subscriber I have the constructor which takes parameters such as name,age,id number etc..
However in my derived classes I have extra parameters such as "schoolName" for example. How do I setup the constructor for the derived class Student knowing that i want to call the base class constructor of Subscriber and also add a value to an extra parameter that being schoolName.
Also my second question. Would it be preferable to make the shared parameters such as age,name etc (that both the base class and the derived class share) protected as apposed to private ? That way a student can access his name,age easily
Base class constructor:
Subscriber(const char *name, int age);
Derived class constructor:
Student(const char *name, int age, const char *sName)
:Subscriber(name, age) // We call the base class constructor
{
// Do something with sName.
}
It depends on what you would like to achieve, but generally it is preferred to keep the parameters as private. In this way there's less coupling between the classes, and if you need access to the parameters of the base class you can use the getter-setter approach.