Suppose we want two constructors for a class representing complex numbers:
Complex (double re, double img) // construct from cartesian coordinates
Complex (double A, double w) // construct from polar coordinates
but the parameters (number and type) are the same: what is the more elegant way
to identify what is intended? Adding a thir...
I know it is a strange question did someone have had a case where a C# object return null after newing it up?
...
Consider the following code:
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var square = new Square(4);
Console.WriteLine(square.Calculate());
}
}
class MathOp
{
protected MathOp(Func<int> calc) { _calc ...
What is the difference between:
long myLong;
float myFloat = (float) myLong;
and:
float myFloat = float(myLong);
...
Well, maybe it is a stupid question, but I cannot resolve this problem.
In my ServiceBrowser class I have this line:
ServiceResolver serviceResolver = new ServiceResolver(ifIndex, serviceName, regType, domain);
And compiler complains about it. It says:
cannot find symbol
symbol : constructor ServiceResolver(int,java.lang.String,java...
Sorry if this is a bit random, but is it good practice to give all fields of a class a value when the class is instanciated? I'm just wondering if its better practice to have a constuctor that takes no parameters and gives all the fields default values, or whether fields that have values should be assigned and others left alone until req...
I have a varargs contructor like this :
public class Sentence {
public String[] str;
public Sentence(Object... text){
StringBuilder sb = new StringBuilder();
for (Object o : text) {
sb.append(o.toString())
.append(" ");
}
System.out.println(sb.toString());
}
}
Con...
Here is an extract from item 56 of the book "C++ Gotchas":
It's not uncommon to see a simple
initialization of a Y object written
any of three different ways, as if
they were equivalent.
Y a( 1066 );
Y b = Y(1066);
Y c = 1066;
In point of fact, all three of these
initializations will probably result
in the same obje...
I have a class with an object as a member which doesn't have a default constructor. I'd like to initialize this member in the constructor, but it seems that in C++ I can't do that. Here is the class:
#include <boost/asio.hpp>
#include <boost/array.hpp>
using boost::asio::ip::udp;
template<class T>
class udp_sock
{
public:
...
While trying to answer this question I found that the code int* p = new int[10](); compiles fine with VC9 compiler and initializes the integers to 0. So my questions are:
First of all is this valid C++ or is
it a microsoft extension?
Is it guaranteed to initialize all
the elements of the array?
Also, is there any difference if I
do new...
I have always been a good boy when writing my classes, prefixing all member variables with m_:
class Test {
int m_int1;
int m_int2;
public:
Test(int int1, int int2) : m_int1(int1), m_int2(int2) {}
};
int main() {
Test t(10, 20); // Just an example
}
However, recently I forgot to do that and ended up writing:
class Te...
I'm not sure how to describe this but I'm trying to create a base class that contains a shared (factory) function called FromXml. I want this function to instantiate an object of the proper type and then fill it via an XmlDocument.
For example, let's say I have something like this:
Public Class XmlObject
Public Shared Function Fro...
I have the following code. Which is "correct" and which I do not understand:
private static void updateGUI(final int i, final JLabel label) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
label.setText("You have " + i + " seconds.");
}
}
);
}
I create ...
I need to create class Dog and PurebredDog extending Dog. Problem is that Dog can be at once single object and array of objects (Dogs and PurebreedDogs :
Dog pack[]={new Dog(76589,"As","black",18,
"Ann","Kowalsky"),
new PurebreedDog(45321,"Labrador","Elf","black",25,
"Angus","Ma...
Consider the following code:
struct Calc
{
Calc(const Arg1 & arg1, const Arg2 & arg2, /* */ const ArgN & argn) :
arg1(arg1), arg2(arg2), /* */ argn(argn),
coef1(get_coef1()), coef2(get_coef2())
{
}
int Calc1();
int Calc2();
int Calc3();
private:
const Arg1 & arg1;
const Arg2 & arg2;
// ...
const...
Hello
Can I call constructor explicitly, without using new, if I already have a memory for object?
class Object1{
char *str;
public:
Object1(char*str1){
str=strdup(str1);
puts("ctor");
puts(str);
}
~Object1(){
puts("dtor");
puts(str);
free(str);
}
};
Object1 ooo[2] = ...
I'm reading a book about java. It just got to explaining how you create a class called "deck" which contains an array of cards as its instance variable(s). Here is the code snippit:
class Deck {
Card[] cards;
public Deck (int n) {
cards = new Card[n];
}
}
why isn't the this. command used?
for example why i...
Is there difference in behavior between a constructor call and a procedure call in Delphi records?
I have a D2010 code sample I want to convert to D2009 (which I am using). The sample uses a parameterless constructor, which is not permitted in Delphi 2009. If I substitute a simple parameterless procedure call, is there any functional dif...
Below I've included my h file, and my problem is that the compiler is not liking my simple exception class's constructor's with initializer lists. It also is saying that string is undeclared identifier, even though I have #include <string> at the top of the h file. Do you see something I am doing wrong? For further explanation, this is o...
So here is my code:
public MyClass (int y) {
super(y,x,x);
//some code
}
My problem is that in this case i want to generate a 'x' and sent to the super constructor. However the call to the superconstructor must be the first line in this constructor. Of course I could do something like this:
int x;
{
x = generateX();
}
B...