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.
Hi,
i am trying to return a new object -constructor of which is protected- from a factory method. The factory method is part of the same namespace, but belongs to no class. It seems, that a pointer to a "protected" object cannot be returned by a "public" method. How can i deal with this? I get the following error:
Code:
Building.h: In function ‘shelter::Building* NewShelter(shelter::ShelterType)’:
Building.h:14: error: ‘shelter::Building::Building()’ is protected
Building.cpp:10: error: within this context
Building.h:
Code:
#ifndef BUILDING_H
#define BUILDING_H
#include <string>
using namespace std;
namespace shelter {
enum ShelterType { HOUSE, BLOCKOFLATS, BUILDING};
class Building {
protected: //will be accessible from deriving classes
Building();
Building(string area, string road, int roadnum, int roomnum);
~Building();
string s_area;
string s_road;
int i_roadnum;
int i_roomnum;
public:
Building(const Building &b);
string GetAreaName();
string GetRoadName();
int GetRoadNum();
int GetRoomNum();
void SetAreaName(string);
void SetRoadName(string);
void SetRoadNum(int);
void SetRoomNum(int);
friend Building* NewShelter(ShelterType t);
};//Building
////////////////////////////////////////////////////////////////
namespace life {
class House: public Building {
protected:
bool b_kitchen;
int i_roomsnum;
public:
bool GetKitchen();
bool GetRoomsNum();
void SetKitchen(bool);
void SetRoomsNum(bool);
};//Building
class BlockoFlats: public Building {
private:
int i_flatsnum;
public:
int GetFlatsNum();
void SetFlatsNum(int);
};//BlockoFlats
}//life
////////////////////////////////////
Building* NewShelter(ShelterType);
}
#endif
Building.cpp:
Code:
#include "Building.h"
using namespace shelter;
Building* NewShelter(ShelterType t) {
return new Building(); //<------ERROR
}//NewShelter
...
HIt seems, that a pointer to a "protected" object cannot be returned by a "public" method. How can i deal with this?
Wrong. Pointer to object with protected constructor CAN be returned (try return 0; or return (Building*)0;). But you can't construct object with protected constructor (you are trying to do that by calling "new Building()").
Make derived class with public constructor (and do "return new DerivedBuiding()") or make Building constructor public.
return a new object -constructor of which is protected- from a factory method. The factory method is part of the same namespace, but belongs to no class.
Those requirements contradict each other.
ErV already explained most of it. But his suggestion might not be best.
Quote:
Originally Posted by ErV
Make derived class with public constructor (and do "return new DerivedBuiding()") or make Building constructor public.
There are a few things likely wrong with the idea of using a public constructor of a derived class, and I assume the OP has some good reason for having no public constructor for the original class.
The best solution is probably a pubic factory method in a derived class (calling the protected constructor in the original class). You probably want to keep the factory method that belongs to no class and have it call the factory method in the derived class.
There are a few things likely wrong with the idea of using a public constructor of a derived class, and I assume the OP has some good reason for having no public constructor for the original class.
The best solution is probably a pubic factory method in a derived class (calling the protected constructor in the original class). You probably want to keep the factory method that belongs to no class and have it call the factory method in the derived class.
Explanation:
As I understand it, protected constructor useful only in situations where base class isn't abstract but shouldn't be created by user. I.e. it is base "abstract" or placeholder class which can be used throughout program, but in reality its' derivatives will be used. So you can create Derived class which public constructor, which won't be visible anywhere else (i.e. class fully declared within *.cpp, no *.h to include), but can be returned by factory method. Anyway, isn't that what the factory methods are for? You declare generic base class, and return dozens of derivatives using factories - concealing tons of declarations and possibly easily extending program later. Listing was clearly a quick test of factory method, so making derived class is probably easiest way to check if it works right.
Anyway, idea of making generic base and returning derived class instead of base through factory method looks fine for me.
Thanks for the help first. Since i am newbie, i am still trying to implement this. Solution has a base -in my space knowledge, i guess. I think, that the creation of "protected" objects through the use of a "protected" constructor works in Java. Can somebody confirm?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.