LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2009, 06:44 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
c++: method is protected withing this context


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
...
 
Old 03-11-2009, 07:11 AM   #2
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by nocturna_gr View Post
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.

Last edited by ErV; 03-11-2009 at 07:13 AM.
 
Old 03-11-2009, 07:52 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by nocturna_gr View Post
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 View Post
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.
 
Old 03-11-2009, 04:06 PM   #4
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by johnsfine View Post
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.

Last edited by ErV; 03-11-2009 at 04:13 PM.
 
Old 03-12-2009, 06:19 AM   #5
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Original Poster
Rep: Reputation: 30
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running Linux from withing Windows lordashraf Linux - Newbie 2 11-23-2008 06:24 AM
Activate V-Sat back up withing 4 seconds rudra_sakti Linux - Newbie 0 10-25-2008 06:18 AM
sed command extract contents withing body tag of html Fond_of_Opensource Linux - Newbie 6 06-04-2007 07:55 AM
SlackWare :Method Not Allowed The requested method POST is not allowed for the URL slack31337 Linux - Software 0 04-08-2006 06:09 PM
Call external prog from withing chroot jail The_JinJ Linux - General 2 12-13-2005 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration