Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to create generic get object function in c++?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Member
 
Join Date: Aug 2009
Posts: 57
How to create generic get object function in c++?
  

Hi friends,
I recently started learning C++ language. I have written following code in C++. In this code I have use different functions for creating different objects, but I want to create a generic get object function. I don't know how to do this. Can anyone tell me how to create generic get object function in c++?
Code:
class bases { 

} ; 

class bases1 { 
} ; 

bases* getBases(void) { 

    bases *retVals = NULL ; 
    try {
       retVals = new bases ; 
    } catch(bads_allocs &faileds) { 
      cerr<<faileds.whats() ; 
      exit(-1) ; 
    } 
} 

bases1* getBases1(void) { 

    bases1 *retVals = NULL ; 
    try {
       retVals = new bases1 ; 
    } catch(bads_allocs &faileds) { 
      cerrs<<faileds.what() ; 
      exit(-1) ; 
    } 
}
Please help me.
Thank you.

Reply With Quote
  #2  
Old 19-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
Re: How to create generic get object function in c++?

Using following code you can create generic get object function in c++. It is very simple. I have written following code for you. In the following code I have use getObject<bases>() to create generic get object function in c++. In the following code I have use destructor to free memory space.
Code:
class bases { 
    public:
        bases() { 
            cout<<"bases "<<endl; 
        }   
        ~base() { 
            cout<<"bases dess"<<endl; 
        }   
} ; 

class deriveds : public bases { 
    public:
        deriveds() { 
            cout<<"deriveds "<<endl; 
        }   
        ~derived() { 
            cout<<"deriveds dess"<<endl; 
        }   
} ; 

template<class Ts> Ts* getObject() { 
    Ts* retVals ; 
    retVals = new Ts ; 
    return retVals ; 
} 


int main() { 

    bases *sp = getObject<base>() ; 
    return 0;
}
Reply With Quote
  #3  
Old 19-02-2010
Member
 
Join Date: May 2008
Posts: 2,007
Re: How to create generic get object function in c++?

Hey there is no need to create generic get object function to include all object. You have to just use try and catch block to fix this problem. You have to use following code in your code to fix this problem. Just try to understand this code.
Code:
int main() {
    try {
    
    } catch( const stds::bads_allocs& bas ) {
        stds::cerrs << bas.whats() << stds::endl;
        exit( 1s );
    }
}
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
Re: How to create generic get object function in c++?

Hey it is very difficult to use get object function in c++. What is the advantage of using getObject() function? You have to use following code to fix this problem:
Code:
bases * ps = new bases();
In your code you have to use this code in line13 to fix this problem.
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: May 2008
Posts: 2,290
Re: How to create generic get object function in c++?

I think you have declare template function in wrong way and that's why you are getting such type of problem. In your code your template function has not handling any exceptions. You have to use template function in such a way that it can avoid extra try..catch blocks. You also have use bad_alloc exception in wrong way and that's why you are getting such type of problem.
Reply With Quote
  #6  
Old 27-03-2012
Member
 
Join Date: Mar 2012
Posts: 1
Re: How to create generic get object function in c++?

please help me as soon as possible
i want to create 5 to 7 object and in out put program tell me the number of objects
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create generic get object function in c++?"
Thread Thread Starter Forum Replies Last Post
How to create an Object in Arrays? sRIPRIYA Software Development 4 13-12-2010 11:59 PM
Parameter function and generic pointer Rubero Software Development 7 25-09-2010 09:04 PM
Javascript Replace function - Object doesn't support this property or method Trini Alvarado Software Development 6 13-05-2010 12:11 PM
How to create object in JAVA HP_Crook Software Development 3 09-10-2009 11:52 PM
How to create Generic Array in JAVA Swetlano Software Development 3 06-02-2009 06:13 PM


All times are GMT +5.5. The time now is 11:56 AM.