class

How to make type cast for python custom class

让人想犯罪 __ 提交于 2020-01-13 10:18:08
问题 Consider a class; class temp: def __init__(self): pass I make an object of it; obj = temp() Convert it to string; strObj = str(obj) Now, how can i convert strObj to an object of temp class?? org = temp(strObj) 回答1: As noted by Anand in comments, what you're looking for is object serialization and deserialization. One way to achieve this is through the pickle (or cPickle) module: >>> import pickle >>> class Example(): ... def __init__(self, x): ... self.x = x ... >>> a = Example('foo') >>>

Do I kill a kitten each time I use struct everywhere instead of class?

筅森魡賤 提交于 2020-01-13 09:04:52
问题 struct is public by default while class is private by default. Lets take Ogre3D for example; if I change all class occurences with struct, it compiles (I guess), and the engine works just as before. If I'm right, the compiled code is exactly the same as before, because it's only the compiler that does check if a private/protected methods are called, it's not checked at runtime. If I'm still right, class is just a keyword that just makes its cute eyes and begging "please encapsulate your data:

Java - Get reference to a static class using reflection

大城市里の小女人 提交于 2020-01-13 08:36:10
问题 In Java, is it possible to access an instance of a static class (nested) using reflection? Supposing I have the following 2 classes defined in the package Package1.SubPackage.SubSubPackage: public class MyMainClass { public static class SalesObjectGrouper1 { public static final GrouperContext CONTEXT = new GrouperContext("MyDate"); } private static class SalesObjectGrouper2 { public static final GrouperContext CONTEXT = new GrouperContext("MyDate"); } } If I run the following code: try { xyz

How do you pass a function of a class as a parameter to another function of the same class

萝らか妹 提交于 2020-01-13 06:47:47
问题 i basically want to use a dif function to extract a different element of a class (ac). the code is similar to this: .h: class MyClass { public: double f1(AnotherClass &); void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)); }; .cc: double MyClass::f1(AnotherClass & ac) { return ac.value; } void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)) { std::cout << f1(ac); } didn't work, it gives error#547 "nonstandard form for taking the address of a member

How to pass a Class as Parameter

点点圈 提交于 2020-01-13 06:14:06
问题 In C++ is there a way to pass a type (e.g. a class) as parameter to a function? Explanation why I need this: There is a abstract data class and a manager class. The manager class contains a list (or in this case a map) of objects of derived classes of data . I use unique_ptr for this as mentioned in the answer of this question. class Data{}; // abstract class Manager { map<string, unique_ptr<Data>> List; }; Now imagine I want to add a new data storage to the manager. class Position : public

query mysql database from inside a class

…衆ロ難τιáo~ 提交于 2020-01-13 04:28:47
问题 I'm trying to run a query to a MySQL database from within a class and it's not working for some reason. I have the class in a separate file which I'm linking to with the require_once() function. here's what the main .php file looks like: <?php require_once("connect.php"); require_once("theClass.php"); $a = new theClass; $a->runQuery(); } connect.php: <?php //connect to mySQL database $mysqli = new mysqli("host", "user", "password", "db"); if ($mysqli->connect_errno) { echo "<br><h1>Failed to

Accessing class variables

独自空忆成欢 提交于 2020-01-13 04:13:26
问题 It's the first time I use OOP with PHP 5.. so this is my problem.. I have a file disp.php that contains a class named class disp (model in MVC) <?php class disp{ public $n_pages; public $current_page; private $cmd2; /***************SQL command generator*******************/ private function getCmd2($cmd1,$id,$first_entry,$perpage,$tri){ $cmd2=str_replace('COUNT(*)','*',$cmd1); $cmd2=$cmd2.' ORDER BY '.$id.' '.$tri.' LIMIT '.$first_entry.','.$perpage; return $cmd2; } /********************Items

How to add std::swap for my template class? [duplicate]

三世轮回 提交于 2020-01-13 03:03:27
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: how to provide a swap function for my class? There are some questions about this, but a lot of contradictions (person A giving solution A' with many upvotes with person B saying it's UB) or "only works if the compiler supports ADL" is answered. So, say I have the following template (container) class: template<typename T> class C { // ... void swap(C<T>& y) throw(); // C x; x.swap(y); } then what is the correct

How to add std::swap for my template class? [duplicate]

那年仲夏 提交于 2020-01-13 03:03:13
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: how to provide a swap function for my class? There are some questions about this, but a lot of contradictions (person A giving solution A' with many upvotes with person B saying it's UB) or "only works if the compiler supports ADL" is answered. So, say I have the following template (container) class: template<typename T> class C { // ... void swap(C<T>& y) throw(); // C x; x.swap(y); } then what is the correct

Adding a class with javascript, not replace

大城市里の小女人 提交于 2020-01-13 02:16:06
问题 I would like the ability to add a class to an element and not replace what classes are already there. Here is my javascript so far: function black(body) { var item = document.getElementById(body); if (item) { item.className=(item.className=='normal')?'black':'normal'; } } That piece of javascript replaces the existing classes with black . If the class already is black then it is changed to normal . I would like to somehow combine the script above with the script below script, which adds a