constructor

How to create a Class with multiple constructors that use the same parameter type

我只是一个虾纸丫 提交于 2019-12-21 18:40:10
问题 I am trying to do something like this: public class Arquivo { private File diretorio = null ; public Arquivo(File dir){ this.diretorio = dir; } public Arquivo(String dir){ this( new File(dir) ); } public Arquivo(String fileName){ this( new File("./src/Data/"+fileName) ); } } 回答1: You can't with constructor, that is one of the limitation of constructors time to start using static factory pattern See Also What are static factory methods? 回答2: You can't create two constructors that receive a

Can't find .dtors and .ctors in binary

喜夏-厌秋 提交于 2019-12-21 17:53:40
问题 I am reading the book Hacking, the art of exploitation. In the book there is a section that explain the use of .dtors and .ctors . I'm trying to reproduce one of the exercises of the book but in my executable I do not have this sections. At first I thought the problem was that I was compiling for 64-bit, but now I'm compiling for 32-bit and .dtors and .ctors are still not appearing in the section table. Here is the code: #include <stdio.h> #include <stdlib.h> static void miConstructor(void) _

Why does this class have two constructors?

隐身守侯 提交于 2019-12-21 17:02:58
问题 I see this in the slide that aims to illustrate constructors. I'm confused now because it has two constructors that have the same job accept setting gpa to zero in the second one. Why does the coder need to repeat this.id = id; this.name = name; again? Why does this class even need two constructors? class Student{ private int id; private String name; private double gpa; public Student(int id, String name, double gpa){ this.id = id; this.name = name; this.gpa = gpa; } public Student(int id,

Illegal Member initialization

无人久伴 提交于 2019-12-21 16:18:11
问题 I am using this pretty simple class without using any inheritance. class A { int a; int b; public: A(int x, int y) { a = x; b = y;} A() :A(0,0){}; ~A(){}; } ; int main () { A a1, a2(5, 7) ; } I get this error. error C2614: 'A' : illegal member initialization: 'A' is not a base or member There are similar questions on SO but they relate to inheritance. Can someone explain the reason and what does standard say about that? EDIT: It would be better if someone elaborate more on the forwarding

Struct constructor calls this()

我的未来我决定 提交于 2019-12-21 12:37:26
问题 I came across the following code snippet and was wondering what the purpose of writing a constructor this way was? public struct DataPoint{ public readonly long X; public readonly double Y; public DataPoint(long x, double y) : this() { this.X = x; this.Y = y; } } Doesn't this() just set X and Y to zero? Is this not a pointless action seeing as afterwards they are immediately set to x and y ? 回答1: public DataPoint(long x, double y) : this() { This calls the default constructor for the struct ,

Scala problem optional constructor

北战南征 提交于 2019-12-21 12:26:59
问题 Imagine this simple piece of code: class Constructor() { var string: String = _ def this(s: String) = {this() ; string = s;} def testMethod() { println(string) } testMethod } object Appl { def main(args: Array[String]): Unit = { var constructor = new Constructor("calling elvis") constructor = new Constructor() } } The result is null null I would like to be calling elvis null How to achieve this? I cannot call the method testMethod after the object creation. Mazi 回答1: Your test method is

Are empty constructors always called in C++?

末鹿安然 提交于 2019-12-21 12:17:46
问题 I have a general question, that may be a little compiler-specific. I'm interested in the conditions under which a constructor will be called. Specifically, in release mode/builds optimised for speed , will a compiler-generated or empty constructor always be called when you instantiate an object? class NoConstructor { int member; }; class EmptyConstructor { int member; }; class InitConstructor { InitConstructor() : member(3) {} int member; }; int main(int argc, _TCHAR* argv[]) { NoConstructor*

Does the memory get released when I throw an exception?

我怕爱的太早我们不能终老 提交于 2019-12-21 12:09:17
问题 I was debating with some colleagues about what happens when you throw an exception in a dynamically allocated class. I know that malloc gets called, and then the constructor of the class. The constructor never returns, so what happens to the malloc ? Consider the following example: class B { public: B() { cout << "B::B()" << endl; throw "B::exception"; } ~B() { cout << "B::~B()" << endl; } }; void main() { B *o = 0; try { o = new B; } catch(const char *) { cout << "ouch!" << endl; } } What

In which cases there is no constructor at all, even a default constructor?

爱⌒轻易说出口 提交于 2019-12-21 11:22:20
问题 In this book I am currently reading I ran across this: A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization. Am I correct in inferring from the above that the compiler does not generate a default constructor for the class/structure in some cases? If yes, what are those cases? I will venture and say POD is probably one. Are there any other? EDIT: I have changed the title as the original title gave the meaning that I asked when was a

Java: NULL in constructor

孤者浪人 提交于 2019-12-21 09:27:10
问题 Please I have this question which is a bit theoretical, but I would like to understand it. Why if I pass a null argument to a constructor do I get a NullPointerException ? This is my example new AttendeeDetail("Gus Goose","1151","15-01-2012",(Integer) null,null) This is the class: public class AttendeeDetail { private String ticketholder_name; private String user_id; private String date_of_birth; private int tickets_purchased; private ArrayList<Ticket> tickets; public AttendeeDetail(String