i have this sample:
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int one) { x = one; }
int getX() { return x; }
};
void main()
{
A first(5);
first = 10;
}
which in here the constructor is called in both lines of main.
but if we had more than one variable in the class, is it possible to call the constructor with the operator =?
like in here:
class A {
public:
int x,y;
A(int one,int sec) { x = one; y=sec;}
int getX() { return x; }
int getY() { return y; }
};
and create a class variable using = like this?
A example=(50,40)
Yes, since C++11 you can do it with copy-list-initialization like this:
A example = {50, 40};
example = {40, 50};
Related
I'm trying to make this code work:
#include <iostream>
using namespace std;
int f(int x) {
return x+1;
}
class A {
public:
int g(int y);
};
int A::g(int y) = f;
int main() {
A test;
cout << test.g(3) << endl;
return 0;
}
It does not compile because of the line int A::g(int y) = f;.
What is the correct way to achieve that an external function can be used as a method?
You can use a pointer to function as a member of class A. Now assign function f to g member of A.
int f(int x) {
return x+1;
}
class A {
public:
int (*g)(int);
};
int main(){
A test;
test.g = f;
cout << test.g(10); // prints 11
}
You can accomplish the same by making your function a callable objects by implementing () operator. so you can have that as member of the class, and then it can normally be used as function on the class objects.
#include <iostream>
struct do_something{
int operator()(int num){
return num;
}
};
class test{
int sum;
public:
do_something fun;
};
int main(){
test obj;
std::cout << obj.fun(10);
}
I'm working on a small assignment for a class. I need to overload the '+' operator to add together two private member variables of a class object. The class objects are pointed to by an array of class pointers. I'm not quite sure where to start debugging, but my operator overload function causes the program to crash.
Here is main.cpp (ignore printObjects and freeObjects for now).
#include <iostream>
#include "Foo.h"
using namespace std;
void createObjects(Foo* fooArr[3], Foo* newFoo);
void printObjects(Foo* fooArr[3], Foo newFoo);
void freeObjects(Foo* fooArr[3], Foo newFoo);
int main()
{
Foo *fooArr[3] = { 0 };
Foo *newFoo = 0;
createObjects(&fooArr[3], newFoo);
Foo f1 = *(fooArr[1]) + *(fooArr[2]);
f1.outputX();
}
void createObjects(Foo* fooArr[3], Foo* newFoo)
{
for (int i = 0; i < 3; i++)
{
newFoo = new Foo(i+1);
fooArr[i] = newFoo;
}
}
Here is Foo.h:
using namespace std;
class Foo
{
private:
int x;
public:
Foo();
Foo(int);
void setX(int);
const int getX();
void outputX();
Foo operator+(const Foo&) const;
};
And finally here is foo.cpp:
#include <iostream>
using namespace std;
#include "Foo.h"
Foo::Foo(int x_)
{
x = x_;
}
void Foo::setX(int x_)
{
x = x_;
}
const int Foo::getX()
{
return x;
}
void Foo::outputX()
{
cout << x << endl;
}
Foo Foo::operator+(const Foo& f2) const
{
Foo f3(0);
f3.x = x + f2.x;
return f3.x;
}
The program crashes (segFault), right after calling the operator+ overload function. Now I know this means I am trying to access memory that the program doesn't actually have access to. However, I just don't know how to craft the operator overload statement to work with the function call:
Foo f1 = *(fooArr[1]) + *(fooArr[2]);
Any help would be much appreciated.
Thanks!
I have found the following example in one of my C++ courses. When I try to compile it I get the following error:
'B::operator A' uses undefined class 'A'
Why does it say that class A is undefined?
#include<iostream>
using namespace std;
class A;
class B
{
int x;
public: B(int i = 107) { x = i; }
operator A();
};
B::operator A() { return x; }
class A
{
int x;
public: A(int i = 6) { x = i; }
int get_x() { return x; }
};
int main()
{
B b;
A a = b;
cout << a.get_x();
system("Pause");
}
The compiler needs to know what A is here:
B::operator A() { return x; }
But you only have a forward declaration. You need to move the declaration of class A above B
You are only allowed to use pointers to or references of incomplete types which is what have when you forward declare a type
You need to declare A above B, so that the definition of A is visible to B.
#include<iostream>
using namespace std;
class A
{
int x;
public: A(int i = 6) { x = i; }
int get_x() { return x; }
};
class B
{
int x;
public: B(int i = 107) { x = i; }
operator A();
};
B::operator A() { return x; }
int main()
{
B b;
A a = b;
cout << a.get_x();
}
This should work.
I have the following code for composition. It generates an error,
#include <iostream>
using namespace std;
class X
{
private:
int iX;
public:
X(int i=0) : iX(i) { cout <<"Constructing X.."<<endl; }
~X() { cout <<"Destructing X.."<<endl; }
int getIX() { return iX; }
};
class Y
{
private:
X x(3);
int jY;
public:
Y(int j = 0) : jY(j) { cout <<"Constructing Y.."<<endl; }
~Y() { cout <<"Destructing Y.."<<endl; }
void callGetX() { cout <<"iX is "<<(x.getIX())<<endl; }
};
int main()
{
Y yObj(1);
yObj.callGetX();
}
Error:
In member Function void Y::callGetX()
'x' undeclared (first use this function)
Is there anything that i have missed?
Can anyone please tell me the constructor call mechanism for this scenario?
X x(3);
This is not legal in C++ (legal in Java AFAIK). In fact it makes the compiler think that x is a member function that returns an object of class X instead of considering x a member variable of class X.
Instead do this:
Y(int j = 0) : jY(j), x(3) { cout <<"Constructing Y.."<<endl; }
You put the member in your initialization list:
Y(int j = 0) : x(3), jY(j)
Your syntax:
class Y
{
private:
X x(3);
//...
};
is illegal.
I'd like to bind operator new (see example below). If the constructor doesn't have any arguments, it works fine, but if it does have arguments, I apparently have trouble getting the bind syntax correct.
#include <map>
#include <boost\function.hpp>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\construct.hpp>
#include <boost\lambda\bind.hpp>
enum TypeEnum
{
BarType,
BazType
};
class Foo
{
};
class Bar : public Foo
{
public:
Bar(int x)
{ BarVal = x; }
private:
int barVal;
};
class Baz : public Foo
{
public:
Baz(int x)
{ bazVal = 2 * x; }
private:
int bazVal;
};
class FooFactory
{
public:
FooFactory()
{
// How does this work?
factoryMap[BarType] = boost::lambda::bind(boost::lambda::new_ptr<Bar>(_1));
factoryMap[BazType] = boost::lambda::bind(boost::lambda::new_ptr<Baz>(_1));
}
Foo* getFoo(TypeEnum type, int z)
{
return factoryMap[type](z);
}
private:
std::map<TypeEnum, boost::function<Foo* (int)>> factoryMap;
};
int main()
{
FooFactory fooFactory;
Bar *newBar = static_cast<Bar*> (fooFactory.getFoo(BarType, 10));
return 0;
}
This should do:
factoryMap[BarType] = boost::lambda::bind(boost::lambda::new_ptr<Bar>(), boost::lambda::_1);
factoryMap[BazType] = boost::lambda::bind(boost::lambda::new_ptr<Baz>(), boost::lambda::_1);
Why not just to write the following? I can't see any reason to use bind in your case.
factoryMap[BarType] = boost::lambda::new_ptr<Bar>();
factoryMap[BazType] = boost::lambda::new_ptr<Baz>();