Is it possible to know the object instance name / variable name from within a class method? For example:
#include <iostream>
using namespace std;
class Foo {
public:
void Print();
};
void Foo::Print() {
// what should be ????????? below ?
// cout << "Instance name = " << ?????????;
}
int main() {
Foo a, ...
#include <iostream>
using namespace std;
class X {
public:
X() {
cout<<"Cons"<<endl;
}
X(const X& x){
cout<<"Copy"<<endl;
}
void operator=(const X& x){
cout<<"Assignment called";...
I have a non-static class in which i have several properties, ie serverURL, serverPort etc, and the class has a constructor. The constructor accepts arguments which it then uses to 'set' the properties, initialising them. Here is the code:
public Server(string newServerAddress, int newServerPort) {
serverAddress = newServerAddr...
I watched a talk by Douglas Crockford on the good parts in Javascript and my eyes
were opened. At one point he said, something like, "Javascript is the only language where good programmers believe they can use it effectively, without learning it." Then I realized, I am that guy.
In that talk, he made some statements that for me, were...
Hello all.
I meet one strange issue.
Public Class MyClass
{
Public MyClass()
{
// Some time the New Com Obj code will crush in Construcor
su.SUEvent += new _IaSystemMgrEvents_SuEventEventHandler(su_SuEvent);
su.SUEventSteps += new _IaSystemMgrEvents_SuEventIemsEventHandler(su_SuEventSteps);
...
Hi all, I know that this is supposedly a super simple question, but I've been struggling with the concept for some time now. My question is, how do you chain constructors in c#? I'm in my first OOP class, so I'm just learning. I don't understand how constructor chaining works or how to implement it, or even why it's better than just do...
Why is this valid
public struct MyStruct
{
public MyStruct(double value)
{
myField = value;
}
private double myField;
public double MyProperty
{
get
{
return myField;
}
set
{
myField = value;
}
}
}
and this is not
public stru...
I know in D2010 they have added support for static constructors and destructors.
Where I can find more information about they: syntax and samples?
...
Hi, I'm just getting the concept of chaining constructors down, but I can't figure out how to chain these two particular constructors together, so I would appreciate it if somebody could help me out.
Thanks!
Constructors
// default constructor
// purpose: initialize data members to zero
// Parameters: none
// returns: none
public Li...
Both syntaxes are equivalent (at least I suppose they are).
let o1 = new Object()
or
let o2 = Object()
Which way do you use more often? What about readability issues?
...
Hi! I just need some help with this program.
The user has to enter in the id,password,the number of max tries & the number of max uses.
And they have to go into a constructor...
Could someone help me pass them into the constructor?
I'm using java.
Thanks!
import java.util.Scanner;
public class LoginPw{
public static void main(String[...
Hello there,
I'm in my first OOP class and I really like it, but on this assignment, I'm not really sure what the best (most efficient, less code, etc.) way to use constructors in this situation? Preferably using constructor chaining.
A little more info:
I would like my default constructor to create & initialize all the objects shown...
Given a class:
class TCurrency {
TCurrency();
TCurrency(long);
TCurrency(const std::string);
...
};
Wrapped with Boost.Python:
class_<TCurrency>( "TCurrency" )
.def( init<long> )
.def( init<const std::string&> )
...
;
Is it possible to create a factory method that appears as a constructor in Python:
...
Hi,
I have an assignment for my first OOP class, and I understand all of it including the following statement:
You should create a class called ComplexNumber. This class will contain the real and imaginary parts of the complex number in private data members defined as doubles. Your class should contain a constructor that allows the...
Given the following code:
#include <iostream>
struct implicit_t
{
implicit_t(int x) :
x_m(x)
{
std::cout << "ctor" << std::endl;
}
~implicit_t()
{
std::cout << "dtor" << std::endl;
}
int x_m;
};
std::ostream& operator<<(std::ostream& s, const implicit_t& x)
{
return s << x.x_m;...
Hi
I have the following recursive function for project euler question no. 74:
chain n | n `elem` xs = length xs
| otherwise = (chain (sumFac n)) : xs
fac n = foldl (*) 1 $ enumFromTo 1 n
sumFac n = sum $ map fac $ decToList n
Except I don't know the correct syntax to construct a list on chain n so that it builds up a list of xs a...
I've always set up metaclasses something like this:
class SomeMetaClass(type):
def __new__(cls, name, bases, dict):
#do stuff here
But I just came across a metaclass that was defined like this:
class SomeMetaClass(type):
def __init__(self, name, bases, dict):
#do stuff here
Is there any reason to prefer one ...
Here's what I've got:
void set::operator =(const set& source)
{
if (&source == this)
return;
clear();
set(source);
}
And here's the error I get:
vset.cxx:33: error: declaration of 'source' shadows a parameter
How do I properly do this?
...
Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or reference-to-const, when it needs to store a copy of the argument(s) in its object either way?
Here is the shortest example I can think of:
struct foo {
bar _b;
foo(bar [const&] b) // pass by value or reference-to-const?
: _b(b) { ...
Hi,
In my application I have to derive some classes from a base one, the problem is that I want to enforce the derived classed to have 3 particular constructor implementation. As c++ don't have virtual pure constructor, it seemed quite desperate (I had to check manually each class implementation to ensure that the particular ctors are i...