constructor

Constructors with inheritance in c++

If you have 3 classes, with arrows going from parent to child classes (i.e. "A -> B" means "B inherits from A": shape -> 2d shape -> circle +----> 3d shape -> sphere When you write your constructor for the circle class, would you ever just initialize the grandparent Shape object and then your current object, skipping the middle cla...

behaviour of the implicit copy constructor / assignment operator

Hello, I have a question regarding the C++ Standard. Suppose you have a base class with user defined copy constructor and assignment operator. The derived class uses the implicit one generated by the compiler. Does copying / assignment of the derived class call the user defined copy constructor / assignment operator? Or do you need to...

Why can't I construct an std::istream_iterator with an unnamed temporary?

g++ allows this construction of an istream_iterator from an ifstream instance: std::ifstream ifstr("test.txt"); std::istream_iterator<std::string> iter1(ifstr); ...but it doesn't allow the same construction with an unnamed temporary: std::istream_iterator<std::string> iter2(std::ifstream("test.txt")); This gives: error: no mat...

optional arguments in haskell

Hello, I have declared my own type: data Book = Bookinfo { bookId :: Int, title :: String } deriving(Show) and now: x = Bookinfo it is all ok, valid statement but making bookId x throws an error. If I would be able to handle errors in Haskell that would be ok but right now I cant do this So ...

variable scope when adding a value to a vector in class constructor

I have a level class and a Enemy_control class that is based off an vector that takes in Enemys as values. in my level constructor I have: Enemy tmp( 1200 ); enemys.Add_enemy( tmp ); // this adds tmp to the vector in Enemy_control enemys being a variable of type Enemy_control. My program crashes after these statements complaining abou...

C# Strange Behavior

I have a custom struct : struct A { public int y; } a custom class with empty constuctor: class B { public A a; public B() { } } and here is the main: static void Main(string[] args) { B b = new B(); b.a.y = 5;//No runtime errors! Console.WriteLine(b.a.y); } When I run the above program, it does ...

Constructors in Programming languages

Why constructor is not considered as member of a class ? Is there any specific reason ? Thanks and regards. ...

template class: ctor against function -> new C++ standard

Hi in this question: http://stackoverflow.com/questions/2779155/template-point2-double-point3-double Dennis and Michael noticed the unreasonable foolishly implemented constructor. They were right, I didn't consider this at that moment. But I found out that a constructor does not help very much for a template class like this one, instead ...

How to initialise a struct-type in the initialisation list?

How can I initilise a structure in the constructor list? Say: struct St{int x, y}; class Foo { public: Foo(int a = 0, int b = 0) : /*here initilise st_foo out of a and b*/ {} private: const St st_foo; }; ...

Is it bad practise to initialise fields outside of an explicit constructor

Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? So its monday and we are arguing about coding practises. The examples here are a litttle too simple, but the real deal has several constructors. In order to initialise the simple values (eg dates to their min value) I have moved the co...

Explicitly typing variables causes compiler to think an instance of a builtin type doesn't have a property, which it does

I narrowed the causes of an AS3 compiler error 1119 down to code that looks similar to this: var test_inst:Number = 2.953; trace(test_inst); trace(test_inst.constructor); I get the error "1119: Access of possibly undefined property constructor through a reference with static type Number." Now if I omit the variable's type, I don't ge...

C# static constructor and GetVersion() any suggestions?

C# static constructor and GetVersion() any suggestions? Hi, I have defined struct like this in separate file OSVERSIONINFO.cs like this: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct OSVERSIONINFO { public static int SizeOf { get { return Marshal.SizeOf (typeof(OSVERS...

How to use a separate class to validate credit card numbers in C#

I have set up a class to validate credit card numbers. The credit card type and number are selected on a form in a separate class. I'm trying to figure out how to get the credit card type and number that are selected in the other class (frmPayment) in to my credit card class algorithm: public enum CardType { MasterCard, Visa, Amer...

Can a constructor in Java be private?

Can a constructor be private? How is a private constructor useful? ...

I'm following Qt Tutorials and got a simple question

If I want to create my own class MyWidget which inherits from QWidget Tutorial tells me to write constructor like this... MyWidget::MyWidget(QWidget *parent) : QWidget(parent){....} I'm wondering what is the role of : QWidget(parent) Does it mean explicit call for QWidget's constructor? ...

C# Custom data type!

After I decided to implement my Int128 in C#, I thought it would be nice to make it look like other dotNet data types.. But I could not implement the following feature: suffix initialization: such as 13L and 0.2D Can I make my own suffix in C#? And if I can not.. how can I initialize it? i.e Int128 a= ?? ...

How to befriend a templated class's constructor?

Why does class A; template<typename T> class B { private: A* a; public: B(); }; class A : public B<int> { private: friend B<int>::B<int>(); int x; }; template<typename T> B<T>::B() { a = new A; a->x = 5; } int main() { return 0; } result in ../src/main.cpp:15: error: invalid use of constructor ...

Passing constructor arguments when using StructureMap

Hello, I'm using StructureMap for my DI. Imagine I have a class that takes 1 argument like: public class ProductProvider : IProductProvider { public ProductProvider(string connectionString) { .... } } I need to specify the "connectionString at run-time when I get an instance of IProductProvider. I have conf...

Why would the assignment operator ever do something different than its matching constructor?

I was reading some boost code, and came across this: inline sparse_vector &assign_temporary(sparse_vector &v) { swap(v); return *this; } template<class AE> inline sparse_vector &operator=(const sparse_vector<AE> &ae) { self_type temporary(ae); return assign_temporary(temporary); } It seems to be mapping...

beginning oop php question: do constructors take the place of getter?

I'm working through this tutorial: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php At first he has you create a setter and getter method in the class: <?php class person{ var $name; function set_name($new_name){ $this->name=$new_name; } function get_name(){ return $...