dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Is this class set up correctly

  1. #1
    Join Date
    Mar 2007
    Posts
    20

    Is this class set up correctly

    Hi all, I am not working with C++ often so I would like your advise on the following class I created. Basically, upon creating an object of this class, the constructor should do lot's of stuff and in the end have populated various members of this class. In terms of speed and efficiency, do you think the class has been set up correctly.

    I changed the original names of functions and variables into generic names, so there could be some typos. Assume that everything is working, and that whatever is not defined in the class, comes from somewhere else by #include.

    Thanks.


    I have the following header file:

    Code:
    class CreateMyObjects
    {
    public:
    	CreateMyObjects(
    		const HFRM &ObjectIn1,
    		const HFRM &ObjectIn2,
    		const HFRM &ObjectIn3,
    		std::map < std::string, std::vector < AnotherObject1 > > &myMapIn1,
    		Date someDate);
    
    	~CreateMyObjects();
    
    	boost::shared_ptr<AnotherObject2> myObject1;
    	boost::shared_ptr<AnotherObject3> myObject2;
    	std::map < std::string, double > myMap1;
    	std::map < std::string, std::map <std::string, std::map < long, double > > > myMap2;
    	std::vector < double > myVector1;
    	std::vector < double > myVector2;
    
    private:
    	const HFRM &obj1_;
    	const HFRM &obj2_;
    	const HFRM &obj3_;
    	std::map < std::string, std::vector < AnotherObject1 > > &obj4_;
    	QuantLib::Date someDate_;
    
    	void function1();
    	void function2();
    	void function3();
    	void function4();
    	void function5();
    };
    And the corresponding code for this class is:

    Code:
    CreateMyObjects::CreateMyObjects(
    	const HFRM &ObjectIn1,
    	const HFRM &ObjectIn2,
    	const HFRM &ObjectIn3,
    	std::map < std::string, std::vector < AnotherObject1 > > &myMapIn1,
    	Date someDate) 
    	: obj1_(ObjectIn1),
    	obj2_(ObjectIn2),
    	obj3_(ObjectIn3),
    	obj4_(myMapIn1),
    	someDate_(someDate)
    {
    	CreateMyObjects::function1();
    	CreateMyObjects::function2();
    	CreateMyObjects::function3();
    	CreateMyObjects::function4();
    	CreateMyObjects::function5();
    }
    
    void CreateMyObjects::function1()
    {
    	CreateMyObjects::myObject1 = createObjectFunction1(obj1_, obj2_, obj3_, someDate_);
    }
    
    void CreateMyObjects::function2()
    {
    	CreateMyObjects::myObject2 = createObjectFunction2(obj1_, obj4_, someDate_);
    }
    
    void CreateMyObjects::function3()
    {
    	CreateMyObjects::myObject3 = createObjectFunction3(obj2_, obj4_);
    }
    
    void CreateMyObjects::function4()
    {
    	CreateMyObjects::myObject4 = createObjectFunction4(obj1_, obj2_);
    }
    
    void CreateMyObjects::function5()
    {
    	CreateMyObjects::myObject5 = createObjectFunction5(obj3_, obj4);
    }

    In my main I use a call like:

    Code:
    CreateMyObjects* myObject;
    myObject = new CreateMyObjects(obj1, obj2, obj3, obj4, someDate);

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is this class set up correctly

    Quote Originally Posted by bar_ba_dos View Post
    In my main I use a call like:

    Code:
    CreateMyObjects* myObject;
    myObject = new CreateMyObjects(obj1, obj2, obj3, obj4, someDate);
    What is the reason for using "new" to create objects?
    Code:
    CreateMyObjects myObject(obj1, obj2, obj3, obj4, someDate);
    If the lifetime of the object is only going to be within the scope of a functional block (in this case, main()), then there is no need to use "new" to create an object. This is C++, not Java or C#.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Question Re: Is this class set up correctly

    I do not know the requirements of your program. Therefore it is very difficult to comment about whether something is done correctly or efficiently or whether there is a better way. I am concerned about the class attributes that are const references. Why are they setup as references instead of instances of those types? I do not see where the original objects are created within the main. That leaves open the possibility of problems with dangling references and memory leaks; especially if the real project is of a larger scale then what we have seen. Be careful about assuming things about efficiency. First you need to focus on what is correct and whether or not you can properly manage and cleanup the memory for the objects.

    Moreover, where is createObjectFunction1 defined and what does it do? What is the purpose of the additional public attributes? I would question why those things are public and without any comments/documentation, I have no idea what they represent. This example looks very strange and without knowing what you are trying to do, I don't know what else to suggest.

  4. #4
    Join Date
    Dec 2011
    Location
    Bucharest, Romania
    Posts
    29

    Re: Is this class set up correctly

    what does it mean ?
    <style color=1>const HFRM &ObjectIn1<\style>

    wouldn`t be more size efficient to use a pointer for a std type instead ? have u defined HFRM ?
    personally and for myself only the expression
    var::vectort<var2> name ;//
    is unknown and it may be written in other language than plain cpp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)