instantiation

Stop C++ Class Instantiation

点点圈 提交于 2019-12-07 14:19:54
问题 Is there a way to stop a C++ class if there is an error in the instantiation? Like, return NULL maybe? Basically I have a wrapper class for MySQL, and the constructor does the connecting, but if the connection fails, I want the object to be, um, useless? PDB::PDB(string _DB_IP, string _DB_USER, string _DB_PASS, string _DB_DB) : _DB_IP( _DB_IP ), _DB_USER( _DB_USER ), _DB_PASS( _DB_PASS ), _DB_DB( _DB_DB ) { mysql_init(&this->mysql); this->connection = mysql_real_connect(&this->mysql, this->

Generic method: instantiate a generic type with an argument [duplicate]

走远了吗. 提交于 2019-12-07 14:04:31
问题 This question already has answers here : Passing arguments to C# generic new() of templated type (14 answers) Closed 6 years ago . I have a generic method that takes in a type T, which i need to be able to call a constructor on that requires a single XmlNode. Currently, I am trying to do that by having an abstract base class that has the constructors I want (plus a parameterless one so i don't have to edit the "subclasses" other than to add the actual subclassing) and constraining by that. If

Activator.CreateInstance() troubles

冷暖自知 提交于 2019-12-07 12:39:26
问题 I have a factory that is supposed to create objects that inherit from class Foo at run-time. I would think that System.Activator.CreateInstance's return type was the same as the type of an object it's creating, but judging from the following error message, its return type is Object. Error 1 Cannot implicitly convert type 'object' to 'cs_sandbox.Foo'. An explicit conversion exists (are you missing a cast?) F:\projects\cs_sandbox\Form1.cs 46 24 cs_sandbox OK, so maybe I am missing a cast, but

Not Understanding Object Instantiation in C#

主宰稳场 提交于 2019-12-07 08:01:16
问题 This post goes to a gap in my understanding of C# classes and why they are preferable to static functions. I am trying to get a List of objects. Each object in the list represents a record in a table. This would be easy to do in a static function. Using a class, I've been able to do it as follows: Calling routine: ListOfBusinesses l = new ListOfBusinesses (); List<Business> b = l.listBusinesses(); The classes: public class Business { public string Bupk { get; set; } public string Bu_name {

proper instantiation & memory management in cocos2d-x

ぃ、小莉子 提交于 2019-12-07 07:02:56
问题 I've been looking for documentation for cocos2d-x but it seems to be really really poor beyond the very basics. I understand that my own classes should inherit from CCObject to be able to use (originally cocoa's) retain / release mechanism, but I'm still confused about what happens when you new something. init is not called automatically. is it OK to call it from inside the constructor? does that alone guarantee that my object will start with a reference count of 1? what is CC_SAFE_DELETE and

Right way to instantiate class in PHP

假如想象 提交于 2019-12-07 05:19:18
问题 I am trying to create a method inside class, that will instantiate class that is currently in. But I would also need that from this method to work correctly in all extended classes. As I have learned from this thread, it's not good to use self keyword for this task. So obvious choice would be using static keyword. But, I've come across different method that also works. Example: class SimpleClass { private $arg; public function __construct( $arg ){ $this->arg = $arg; } public function getArg()

What is a robust way of template specialization in C++ for separated header/source

坚强是说给别人听的谎言 提交于 2019-12-07 03:53:46
问题 In moderate-sized or even big complex projects separating template declaration and definition is useful to reduce compilation time. However, in a complex code small programmer mistakes may lead to unnoticed behaviour change, e.g. a generic version is called instead of a specialization. Example: Template specialization became invisible due to a missed declaration. ///////////////////// file A.hpp ///////////////////// #include <iostream> template <typename T> class A { public: void foo() { std

instantiateViewControllerWithIdentifier seems to call viewdidload

北城以北 提交于 2019-12-07 02:02:38
问题 I am trying to push a ViewController programmatically into a navigation controller, And I'm using my storyboard to create it. here is my code : + (void) pushViewController:(NSString *) identifier ForItems:(NSMutableArray *) items sender:(UIViewController *) sender { GenericViewController *viewController = (GenericViewController *)[sender.storyboard instantiateViewControllerWithIdentifier:identifier]; viewController.items = [[NSMutableArray alloc] init]; [viewController.items removeAllObjects]

Why does the compiler try to instantiate a template that I don't actually instantiate anywhere?

大兔子大兔子 提交于 2019-12-06 17:48:18
问题 Updated below . The following is the entire code I have in my main.cpp: template<class T> struct other_traits; template<class T> struct some_traits{ typedef decltype(&T::operator()) Fty; typedef typename other_traits<Fty>::type type; }; int main(){ } But I get the following errors with Visual Studio 2010 while g++ compiles just fine: src\main.cpp(9): error C2146: syntax error : missing ';' before identifier 'type' --src\main.cpp(10) : see reference to class template instantiation ' some

Compiler instantiates the function in a template class even without invoking it

无人久伴 提交于 2019-12-06 16:53:05
I had a wrong perception that template function in a class is instantiated only if it's invoked. See the below simple code: template<typename T> struct A { T *p; T& operator * () { return *p; } }; int main () { A<int> ai; // ok int i = *ai; // works fine A<void> av; // compiler complains even "*av" is not called } Just while declaring A<void> , compiler errors out as: error: forming reference to void I tried to specialize the function for void outside the template as below: template<> void A<void>::operator * () {} But it doesn't help and gives error as: error: no member function ‘operator*’