Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Is there a way to fetch objects w/o default constructor?
Is there a way to fetch objects w/o default constructor? [message #18052] Mon, 08 September 2008 11:25 Go to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Simplified code as a description of problem:
class Project
{
    Project(TabCtrl &tabs) {/*add new tab with Project controls*/}
};

class App
{
    void Serialize (Stream &stream) {stream % projects;} //ERROR: no default constructor available
    Array<Project> projects;
    TabCtrl        mainTabs;
};


Is there a way I can fetch my Project objects with auto-adding to the tabs?
Re: Is there a way to fetch objects w/o default constructor? [message #18054 is a reply to message #18052] Mon, 08 September 2008 19:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Mon, 08 September 2008 05:25

Simplified code as a description of problem:
class Project
{
    Project(TabCtrl &tabs) {/*add new tab with Project controls*/}
};

class App
{
    void Serialize (Stream &stream) {stream % projects;} //ERROR: no default constructor available
    Array<Project> projects;
    TabCtrl        mainTabs;
};


Is there a way I can fetch my Project objects with auto-adding to the tabs?


Yes, but not really recomended Smile

The trick would be to implement your own Array serialization, using "new" variant of Add.

Something like:

if(s.IsLoading())
    projects.Clear();
int count = projects.GetCount();
s % count;
for(int i = 0; i < count; i++)
    if(s.IsStoring())
        s % projects[i];
    else
        s % projects.Add(new Project(tabs));


(I would rather find some other solution. IMO, except special cases, classes should have default constructors).

Mirek
Re: Is there a way to fetch objects w/o default constructor? [message #18059 is a reply to message #18054] Tue, 09 September 2008 08:47 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

So what "right", or "typical" solution of such a common problem is recommended? Just to make default constructor along with something like void Setup(.....) ?

stream % projects;

if(stream.IsLoading())
{
    for (int i=0; i<projects.GetCount(); ++i)
        projects.Setup(......);
}


This method has one big drawback: I can`t hold any references inside Project class because I can`t initialize them in constructor. In my previous example this means i.e. I can`t hold tabs reference inside Project which is very uncomfortable. Using simple pointer and setting it inside Setup() is no good too.
Re: Is there a way to fetch objects w/o default constructor? [message #18062 is a reply to message #18059] Tue, 09 September 2008 08:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Tue, 09 September 2008 02:47

So what "right", or "typical" solution of such a common problem is recommended? Just to make default constructor along with something like void Setup(.....) ?



Well, that is what I would use.

Quote:


stream % projects;

if(stream.IsLoading())
{
    for (int i=0; i<projects.GetCount(); ++i)
        projects.Setup(......);
}




Well, I actually do not really know what degree of flexibility is needed... Is the number of projects fixed and only the content is changing? Too little info to discuss the solution.

Quote:


This method has one big drawback: I can`t hold any references inside Project class because I can`t initialize them in constructor. In my previous example this means i.e. I can`t hold tabs reference inside Project which is very uncomfortable. Using simple pointer and setting it inside Setup() is no good too.



That is correct. Personally I would do this sacrifice in order to avoid "Add(new)" variant. (Well, as you might have noticed, I tend to judge the quality of code by number of new/delete statements Smile

Mirek
Re: Is there a way to fetch objects w/o default constructor? [message #18063 is a reply to message #18062] Tue, 09 September 2008 09:38 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

luzr wrote on Tue, 09 September 2008 10:57

Quote:


This method has one big drawback: I can`t hold any references inside Project class because I can`t initialize them in constructor. In my previous example this means i.e. I can`t hold tabs reference inside Project which is very uncomfortable. Using simple pointer and setting it inside Setup() is no good too.



That is correct. Personally I would do this sacrifice in order to avoid "Add(new)" variant. (Well, as you might have noticed, I tend to judge the quality of code by number of new/delete statements Smile



Thanks. I think I`ll store these objects pointers in Ptr objects. This would at least look better than plain pointers. Rolling Eyes
Re: Is there a way to fetch objects w/o default constructor? [message #18064 is a reply to message #18063] Tue, 09 September 2008 09:48 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Tue, 09 September 2008 03:38

luzr wrote on Tue, 09 September 2008 10:57

Quote:


This method has one big drawback: I can`t hold any references inside Project class because I can`t initialize them in constructor. In my previous example this means i.e. I can`t hold tabs reference inside Project which is very uncomfortable. Using simple pointer and setting it inside Setup() is no good too.



That is correct. Personally I would do this sacrifice in order to avoid "Add(new)" variant. (Well, as you might have noticed, I tend to judge the quality of code by number of new/delete statements Smile



Thanks. I think I`ll store these objects pointers in Ptr objects. This would at least look better than plain pointers. Rolling Eyes


That is only useful if they can get destructed while "parent" object is alive....

Mirek
Re: Is there a way to fetch objects w/o default constructor? [message #18073 is a reply to message #18064] Tue, 09 September 2008 13:45 Go to previous message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Plus auto-initialisation to NULL. One less thing to forget is always nice Smile
Previous Topic: Full Support for timezone/DST [FEATURE REQUEST]
Next Topic: CR visibile with notepad
Goto Forum:
  


Current Time: Fri Mar 29 08:53:04 CET 2024

Total time taken to generate the page: 0.02431 seconds