constructor-overloading

cannot appear in a constant expression

孤街浪徒 提交于 2019-12-11 14:55:55
问题 In the following c++ programm: class matrix { public: int n; double **x; matrix(int n) : n(n) { x=new double[n][n]; for (int i=0;i<n;i++) { for(int j=0;j<n;j++) { x[i][j]=0; } } } ... I get the following error: "'n' cannot appear in a constant-expression". Since im relatively new to cpp i dont really know why this error occurs (especially because i did almost the exact same thing with a class called vector and there it was no problem at all) and how to fix it. I would really appreciate any

“Overloading” constructors with SFINAE

一个人想着一个人 提交于 2019-12-10 19:42:06
问题 Why does the the following attempt at overloading the constructor Foo::Foo fail? Also, I'd appreciate alternatives/workarounds #include <vector> #include <type_traits> namespace xyz { struct MemoryManager{}; template<typename T , typename Alloc=MemoryManager> class vector { }; } template<typename T, template<typename,typename> class V , typename A> struct Foo { template<typename U = T , typename Dummy = typename std::enable_if< std::is_same<std::vector<U>, V<U,A> >::value >::type > Foo() //

Why is my overloaded C++ constructor not called?

为君一笑 提交于 2019-12-09 15:52:22
问题 I have a class like this one: class Test{ public: Test(string value); Test(bool value); }; If I create an object like this: Test test("Just a test..."); The bool constructor is called! Anyone knows why? Thanks 回答1: The type of "Just a test..." is const char * , which can be implicitly converted to either bool or std::string . Because std::string isn't a built in type, the const char * s is converted to a bool . You can prevent that by explicitly converting the const char * to a std::string :

Python: Problem with overloaded constructors

我只是一个虾纸丫 提交于 2019-12-07 01:24:42
问题 WARNING: I have been learning Python for all of 10 minutes so apologies for any stupid questions! I have written the following code, however I get the following exception: Message File Name Line Position Traceback Node 31 exceptions.TypeError: this constructor takes no arguments class Computer: name = "Computer1" ip = "0.0.0.0" screenSize = 17 def Computer(compName, compIp, compScreenSize): name = compName ip = compIp screenSize = compScreenSize printStats() return def Computer(): printStats(

Scala overloaded constructors and super

会有一股神秘感。 提交于 2019-12-05 13:32:41
I can't understand how to develop Scala code similar to the following on Java: public abstract class A { protected A() { ... } protected A(int a) { ... } } public abstract class B { protected B() { super(); } protected B(int a) { super(a); } } public class C extends B { public C() { super(3); } } while it's clear how to develop C class, I can't get how to receive B. Help, please. P.S. I'm trying to create my own BaseWebPage derived from wicket WebPage which is a common practice for Java Do you mean something like: abstract class A protected (val slot: Int) { protected def this() = this(0) }

Python: Problem with overloaded constructors

非 Y 不嫁゛ 提交于 2019-12-05 04:31:05
WARNING: I have been learning Python for all of 10 minutes so apologies for any stupid questions! I have written the following code, however I get the following exception: Message File Name Line Position Traceback Node 31 exceptions.TypeError: this constructor takes no arguments class Computer: name = "Computer1" ip = "0.0.0.0" screenSize = 17 def Computer(compName, compIp, compScreenSize): name = compName ip = compIp screenSize = compScreenSize printStats() return def Computer(): printStats() return def printStats(): print "Computer Statistics: --------------------------------" print "Name:"

Why is template constructor preferred to copy constructor?

余生长醉 提交于 2019-12-03 10:34:18
问题 #include <iostream> struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int" << std::endl; } uct( int &&) { std::cerr << "int" << std::endl; } template <typename T> uct(T &&) { std::cerr << "template" << std::endl; } }; int main() { uct u1 ; // default uct u2( 5); // int uct u3(u1); // template, why? } coliru Template overload of the constructor

What does “this()” method mean?

懵懂的女人 提交于 2019-11-28 17:46:35
I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing. public Digraph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); addEdge(v, w); } } I understand what this.method() or this.variable are, but what is this() ? Avi This is constructor overloading: public class Diagraph { public Diagraph(int n) { // Constructor code } public Digraph(In in) { this(in.readInt()); // Calls the constructor above. int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in

C++ how to generate all the permutations of function overloads?

孤者浪人 提交于 2019-11-27 19:49:11
Lets say I have classes Date and classes Year , Month and Day . struct Date { Date(Year year, Month month, Day day) : d(day), m(month), y(year) {}; Date(Month month, Day day, Year year) : d(day), m(month), y(year) {}; Date(Day day, Month month, Year year) : d(day), m(month), y(year) {}; Date(Day day, Year year, Month month) : d(day), m(month), y(year) {}; ... ... private: Day d; Month m; Year y; } This allows me not to have a specific layout of arguments for Date as I have a lot of overloadings. Am I able to generate all the permutations/overloadings automatically? Just to be clear:

C# constructors overloading

南笙酒味 提交于 2019-11-27 14:55:55
How I can use constructors in C# like this: public Point2D(double x, double y) { // ... Contracts ... X = x; Y = y; } public Point2D(Point2D point) { if (point == null) ArgumentNullException("point"); Contract.EndContractsBlock(); this(point.X, point.Y); } I need it to not copy code from another constructor... You can factor out your common logic to a private method, for example called Initialize that gets called from both constructors. Due to the fact that you want to perform argument validation you cannot resort to constructor chaining. Example: public Point2D(double x, double y) { //