constructor

How do I make define and declare a variable using the default constructor in C++?

可紊 提交于 2019-12-23 22:39:33
问题 From my understanding of declarations and definitions, at the global scope: MyClass instance();//Declares a function that returns a MyClass MyClass instance;//Declares an instance of MyClass Is it possible to declare a variable and define it to use the default constructor at global scope? What if I was using a structure instead of a class? EDIT: Okay, so MyClass instance; does call the default constructor. Can anyone explain how this is consistent with this example: int a; // not default

What is the default access of constructor in c++

亡梦爱人 提交于 2019-12-23 20:25:29
问题 What is the default access of constructor in c++ and why ? public, private or protected ? And how do I check it through the code ? 回答1: If you do not declare a constructor yourself, the compiler will always generate a public trivial one for you. They will also implicitly create a public copy constuctor and copy assignment operator. From c++ standard 12.1.5: If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An

Javascript `new` operator & prototype

ⅰ亾dé卋堺 提交于 2019-12-23 20:24:59
问题 Say we create a Function named 'Shape', and add a property 'name' & method 'toString' on it's prototype: var Shape = function () {}; Shape.prototype.name = 'Shape'; Shape.prototype.toString = function () { return this.name; } console.log(Shape.name); // '' console.log(Shape.toString()); // function () {} var Triangle = new Shape(); console.log(Triangle.name); // 'Shape' console.log(Triangle.toString()); // 'Shape' 'NEW' operator did two things: 1. It points this to 'Triangle' 2. It points

c2512 error: no appropriate default constructor available

元气小坏坏 提交于 2019-12-23 20:18:34
问题 I have encountered "c2512" error even if I declared the constructor. My code is like this: in my "first.h" file, I declared it like: class myClass { public: tmpM ( cv::Mat& _model ); } then in my "first.cpp" I did: #include "first.h" myClass::tmpM ( cv::Mat& _model ) { ... } Then I included this "first.h" in my "second.h", then included this "second.h" in my "third.h", and called this class in my "third.cpp" like this: cv::Mat myMat ( height, width, CV_8UC3 ); tmpM aM ( myMat ); But this

Difference between initialization at declaration and initialization in constructor [duplicate]

落花浮王杯 提交于 2019-12-23 20:06:15
问题 This question already has answers here : Initialize class fields in constructor or at declaration? (14 answers) Closed 6 years ago . What is the difference between the following two, and which is more preferable?? public class foo { int i = 2; } public class foo { int i; foo() { i = 2; } } 回答1: In your example, there is no difference in behavioural semantics. In Java, all instance field initializers (and instance blocks) are executed after superclass initialization, and before the body of the

Special member functions in C++0x

落爺英雄遲暮 提交于 2019-12-23 19:47:43
问题 The Wikipedia article about special member functions doesn't contain any reference to move constructors and move assignment operators. I would like to update the entry but I'm not sure what the 0x standard says. What are the rules regarding these two functions? Are they automatically generated by the compiler and if so when? Edit: I've updated the Wikipedia page, if anyone feels like it please help the community by editing it into shape (if needed). 回答1: Keeping in mind C++0x isn't quite

Calling subclass constructor from static base class method

北战南征 提交于 2019-12-23 18:21:24
问题 Ok... in Objective C you can new up a subclass from a static method in the base class with 'new this()' because in a static method, 'this' refers to the class, not the instance. That was a pretty damn cool find when I first found it and I've used it often. However, in C# that doesn't work. Damn! So... anyone know how I can 'new' up a subclass from within a static base class method? Something like this... public class MyBaseClass{ string name; public static Object GimmeOne(string name){ //

Java Setter and Constructor confusion

▼魔方 西西 提交于 2019-12-23 18:16:52
问题 I am a bit confused about how to use constructor and setter in Java, please see the sample code below: public class Name { private String name; public void setName(String name){ this.name=name; } public String getName(){ return name; } } public static void main(String[] args) { Name a=new Name(); a.setName("123"); System.out.println(a.getName()); } It prints out 123, it is using setter method without constructor, I also wrote the other code below: public class Name { private String name;

Explicitly defaulted constructors and initialisation of member variables

梦想与她 提交于 2019-12-23 18:06:07
问题 I'm slightly confused about what happens when a ctor is explicitly defaulted. Are the two code samples below equivalent? Are there any constraints on Y to be able to use the first option? class X { public: X() = default; private: Y m_y; }; class X { public: X() : m_y() {} private: Y m_y; }; 回答1: There are two possible sources of differences. X() = default; is not user-provided . X() : m_y() {} is. The former can be trivial ; the latter is never trivial. Also, they will behave differently if

copy construtor called extra

我是研究僧i 提交于 2019-12-23 18:04:03
问题 I have a program which is showing weird behaviour #include <cstdlib> #include <iostream> using namespace std; class man{ int i ; public: man(){ i=23; cout << "\n defaul constructir called\t"<< i<<"\n"; } man (const man & X) { i = 24; cout << "\n COPY constructir called\t"<< i<<"\n"; } man & operator = (man x ) { i = 25; cout << "\n = operator called\t"<< i<<"\n"; return *this; } }; int main(int argc, char *argv[]) { man x; cout <<"\n ----------\n"; man y = x; cout <<"\n ----------\n"; x=y;