Questions tagged [object-construction]

For questions related to object construction, usually in an OOP environment.

Construction is a concept of Object-Oriented languages.

Objects need to be setup for use:

  • Their member variables need to be allocated
  • Any Object member variables also need to be constructed
  • The Object methods need to be setup for calling

This process is accomplished by construction.

95 questions
5
votes
1 answer

Type coercion in Perl6 class attribute

Like most things in Perl5, there are many ways to create a class that supports custom type coercions for its attributes. Here's a simple one, from an array reference to a hash: #!/usr/bin/env perl package Local::Class { use Moo; use…
jja
  • 2,058
  • 14
  • 27
5
votes
1 answer

Initialize Derived class member variable before calling Base class constructor. Is this UB?

I would like to initialize a member variable of a Derived class, and after that pass it to the Base class constructor. I came up with the solution below (also here: http://cpp.sh/4uu4q) 1) Does the following code have a defined or an undefined…
Iurii Sorokin
  • 115
  • 1
  • 5
4
votes
1 answer

Creational Patterns for Dependency Injection

I'm specifically using C# with Ninject but the problem stretches beyond just Ninject. My problem is that I have a few classes all with different constructor arguments plus the injected ones. I know I can use kernel.Get(constructor args…
4
votes
3 answers

Speeding up Hibernate Object creation?

We use Hibernate as our ORM layer on top of a MySQL database. We have quite a few model objects, of which some are quite large (in terms of number of fields etc.). Some of our queries requires that a lot (if not all) of the model objects are…
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
4
votes
3 answers

Java bad practice doing: new... ().doSomething()?

I just saw a piece of code that had some classes with only one method. I picked an examples: public class TempDirCleanupProcess { public void cleanup(final File directory) {} } Then, later on in the code the method was called the following…
ItFreak
  • 2,299
  • 5
  • 21
  • 45
4
votes
3 answers

Why can compiler only implicitly convert char * to std::string in some cases

These work: struct WithString { WithString(std::string){}; }; void takeString(std::string){} //implicit conversions: takeString("hello"); WithString("hello"); But this does not: WithString makeWithString() { return "hello";} // error: no…
johnbakers
  • 24,158
  • 24
  • 130
  • 258
4
votes
4 answers

Create generic class with internal constructor

Is it possible to construct an object with its internal constructor within a generic method? public abstract class FooBase { } public class Foo : FooBase { internal Foo() { } } public static class FooFactory { public static TFooResult…
David Neale
  • 16,498
  • 6
  • 59
  • 85
4
votes
2 answers

When is a stack object within a function call constructed?

Say I have a simple function: void foo(int val) { if(val == 0) { return; } else { stringstream ss; ss << "Hello World" << endl << ends; cout << ss.str(); } } If I call the function with val == 0, does the…
PentiumPro200
  • 641
  • 7
  • 23
4
votes
4 answers

Create derived class in base class based on parameter

My question is more or less identical to the one at Need a design pattern to remove enums and switch statement in object creation However I don't see that the abstract factory pattern suits well here. I'm currently planning the…
sigy
  • 2,408
  • 1
  • 24
  • 55
3
votes
2 answers

F# Object Tree Syntax

In C# it is possible to construct object trees in a rather succint syntax: var button = new Button() { Content = "Foo" }; Is there an idiomatic way to do something similar in F#? Records have nice syntax: let button = { Content = "Foo" } Object…
Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63
3
votes
3 answers

Does the JVM internally instantiate an object for an abstract class?

I have an abstract class and its concrete subclass, when I create an object of subclass it automatically calls the super constructor. Is the JVM internally creating an object of the abstract class? public abstract class MyAbstractClass { public…
kavi temre
  • 1,321
  • 2
  • 14
  • 21
3
votes
1 answer

PHP Security Vulnerability, Unvalidated Post data used for object instantiation

I'm looking at some basic PHP code that takes a string from POST data via an ajax call. The POST data is used to directly instantiate a class whose name is equivalent to the value of the POSTed string. Assuming that all the class autoloader does is…
RoJG
  • 49
  • 3
2
votes
1 answer

Singletons and factories with multiple libraries in C++

I've been reading and searching the web for a while now but haven't found a nice solution. Here's what I want to do: I am writing a library that defines an abstract base class - lets call it IFoo. class IFoo { public: virtual void…
2
votes
4 answers

Which is more efficient in Qt: constructors with arguments or default constructors with setters afterwards?

The question is as in the title. For example: QPropertyAnimation *animation; animation = new QPropertyAnimation(this, "windowOpacity", this); or QPropertyAnimation…
Subaru Tashiro
  • 2,166
  • 1
  • 14
  • 29
2
votes
5 answers

c++ this pointer question

here is the thing, I want to (probably not the best thing to do) have the ability to call some class constructor that receives as a parameter a pointer to the class who's calling (ufff!!!). Well in code looks better, here it goes, as I do it in…
Yoss
  • 428
  • 6
  • 16