How to get rid of the ugly record - c++

class c {
private:
int n[10];
public:
c();
~c();
int operator()(int i) { return n[i];};
};
class cc {
private:
public:
c *mass;
cc();
~cc();
c& operator*() const {return *mass;};
};
int somfunc() {
c *c1 = new c();
cc * cc1 = new cc();
(*cc1->mass)(1);
delete c1;
}
I've got a pointer into class cc to class c.
Is there any way to get rid of record like this:
(*cc1->mass)(1);
and write somethink like that:
cc1->mass(1);
is it impossible?

When I saw the tags "c++" and "operator overloading", my mind alarm turns ON.
C++ operator overloading is complex, and some operators like "()" or "->" make it more difficult.
I suggest, before overloading operators, making either a global function or method with the same purpouse, test it works, and later replace it with the operator.
Global friend function example:
class c {
private:
int n[10];
public:
c();
~c();
// int operator()(int i) { return n[i]; }
// there is a friend global function, that when receives a "c" object,
// as a parameter, or declares a "c" object, as a local variable,
// this function, will have access to the "public" members of "c" objects,
// the "thisref" will be removed, when turned into a method
friend int c_subscript(c thisref, int i) ;
};
int c_subscript(c* thisref, int i)
{
return c->n[i];
}
int main()
{
c* objC() = new c();
// do something with "objcC"
int x = c_subscript(objC, 3);
// do something with "x"
return 0;
} // int main(...)
Local function ( "method" ) example:
class c {
private:
int n[10];
public:
c();
~c();
// int operator()(int i) { return n[i]; }
int subscript(int i) ;
};
int c::subscript(int i)
{
return this.n[i];
}
int main()
{
c* objC() = new c();
// do something with "objcC"
int x = c->subscript(objC, 3);
// do something with "x"
return 0;
} // int main(...)
And, finally use the overloaded operator:
class c {
private:
int n[10];
public:
c();
~c();
int subscript(int i) ;
int operator()(int i) { return this.subscript(i); }
};
int c::subscript(int i)
{
return this.n[i];
}
int main()
{
c* objC() = new c();
// do something with "objcC"
int x = c->subscript(3);
// do something with "x"
int x = c(3);
// do something with "x"
return 0;
} // int main(...)
Note that in the final example, I keep the method with a unique identifier.
Cheers.

Could always do this:
class cc {
private:
c *_mass;
public:
c& mass() const {return *_mass;};
};
Now..
cc1->mass()(1);

If mass were an object, not a pointer, you could use the syntax you want:
class cc {
private:
public:
c mass;
cc();
~cc();
const c& operator*() const {return mass;};
};
…
cc1->mass(1);

You can with
(*(*cc1))(1)
because operator() is applied to an object, not a pointer.

You can use
(**cc1)(1);
Or
cc1->mass->operator()(1);

Related

How to update a pointer member variable in C++

This is the rough outline of my code. I've left out some details so let me know if you need more context.
class A;
class B;
class C {
public:
C(int dat, int idx) {
data = dat;
i = idx;
}
friend class A;
friend class B;
private:
int data;
int i;
};
class A {
public:
void insert(int dat, int idx) {
c = new C(dat, idx);
// ... other operations to place it
}
// I have the correct destructor, etc..
// function that returns an address to a specific instance of a dynamically allocated C
C* getPointer(int dat) const {
return getPointer(dat); // a call to an internal function
// omitted for simplicity because this works
}
private:
C* c;
};
class B {
public:
B() {
size = 5;
arr = new D[size];
}
// I have the correct destructor, etc..
C* getPtr(int idx) {
return(arr[idx].X);
}
void setPtr(C*& oldPtr, C* newPtr) {
oldPtr = newPtr;
}
private:
struct D{
int d;
C* X;
D() {
d = 0;
X = nullptr;
}
};
D* arr;
int size;
};
int main() {
A a;
B b;
// index is a value returned from an insert function in class B
// dat is a value to find which address should be returned of the objects in class A
a.setPtr(b.getPtr(index), a.getPointer(dat));
return 0;
}
I wrote a function (getPointer(int dat) in class A) that returns the address of an instance of the dynamically allocated object of class C. So, how do I update X to point at this address instead of null?
I've tried many different ways and currently (what I thought was the most promising) was the setPtr() function in class B. It correctly changes the address X is pointing to once, but when I call any other functions its value is back to nullptr.
Any help or advice would be greatly appreciated. Thank you.
You correctly wrote getPtr(int idx) (if I understand your spec. correctly). Now, the return value is C* there, which is not C*& or C**, so it won't be updated if you change the result afterwards - think of it as a return i; won't expose int i; member if return type is int.
So either:
you need to change the return type of getPtr(int idx) and then setPtr() is not really needed;
or you can build both of these in setPtr() as:
void setPtr(int idx, C* newPtr) {
arr[idx].X = newPtr;
}

How to code operation overload so it operates to a specific class member such as A<<{{1,2,3},{5,6,7}} in c++?

You can redefine operator << in class by overload it.
However, how do you code it so that it would operates specific to a certain class member?
for example
class C
{
int a;
double b;
}
// I would like something like
void main ()
{
C c;
c.a << 1; // sets class member a to value 1;
}
I want a operator defined in Class C that operates specifically to class member a.
a pesudo-code would be
class C
{
int a;
double b;
void operator << (istream & fin)
{
... fin.get()... some code
}
}
Stating the obvious for a moment, assuming the variable is public, you'd use:
class C
{
int a;
double b;
}
// I would like something like
void main ()
{
C c;
c.a = 1; // sets class member a to value 1;
}
The << and >> operators are bit shifts, which have their own meaning. Overloading those for your own purpose is probably a bad idea.
The C++ way of doing things is to avoid setting member variables externally where possible (e.g. using RAII approaches, to set data at initialisation)....
class C
{
public:
C(int a, double b) : a(a), b(b) {}
int getA() const { return a; }
double getB() const { return b; }
private:
int a;
double b;
};
.... Or by adding a setter method if you really need it, e.g.
class C
{
public:
C(int a, double b) : a(a), b(b) {}
int getA() const { return a; }
double getB() const { return b; }
void setA(int v) { a = v; }
void setB(double v) { b = v; }
private:
int a;
double b;
};
You could in theory generate a new type, and overload the operators for that type, but it's not something I'd recommend (because changing the meaning of an operator is almost always a bad idea)
struct MyIntType {
int i;
// overload cast operator
operator int () {
return i;
}
// assign
MyIntType& operator = (const int& v) {
i = v;
return *this;
}
// not recommended :(
MyIntType& operator << (const int& v) {
i = v;
return *this;
}
};
class C
{
public:
MyIntType a;
double b;
};
void main ()
{
C c;
c.a << 1;
}
Having read your comment above, it sounds like you want to do this:
class C
{
public:
// I'm still not recommending this :(
C& operator << (const int& v) {
a = v;
return *this;
}
private:
int a;
double b;
};
void main ()
{
C c;
c << 1; //< now sets c.a
}

c++ create a class, call its funcion and push back to vector in one statement

I have a class MyClass and another class that holds an array of MyClass, as follows:
class MyClass {
int a;
float b;
void SetInt(int value)
{
a = value;
}
void SetFloat(float value)
{
b = value;
}
}
class MyClassArray {
std::vector<MyClass> classList;
}
What is the easier way to create a new MyClass, insert an object in MyClassArray and call the methods to store value on it ?
Can I just create a temporary MyClass and insert it on the vector, calling the function in one statement ? Like:
classList.push_back(MyClass().SetInt(21));
classList.push_back(MyClass().SetFloat(1.23));
Is that valid ?
BTW: I need in vector a one object MyClass with 21 set on a and another one with 1.23 set on b, that´s why I´m not using initializers for a and b.
you can use chaining :
class MyClass {
int a;
float b;
public:
MyClass& Set(int value) { a = value; return *this; }
MyClass& Set(float value) { b = value; return *this; }
};
this enables thing like:
MyClass a;
a.Set(1).Set(1.5f);
and also:
vector<MyClass> vec;
vec.push_back(MyClass{}.Set(3));
Use your constructor
class MyClass {
int a;
float b;
}
class MyClassArray {
std::vector<MyClass> classList;
classList.push_back(MyClass(21,1.23));
}
You can use constructors for this. If you overload the constructor to take either an int or a float you would be able to set the value for both of the situations that you outlined.
class MyClass
{
int a;
float b;
MyClass(int i) : a(i) { }
MyClass(float f) : b(f) { }
}
this way you could add objects to the vector by doing this:
std::vector<MyClass> classList;
classList.push_back(MyClass(21));
and by doing this:
classList.push_back(MyClass(1.23));
If you really need to call a separate method you could do it like this:
class MyClass {
int a;
float b;
MyClass& SetInt(int value)
{
a = value;
return *this;
}
MyClass& SetFloat(float value)
{
b = value;
return *this;
}
}
Which will return a reference to the class. It is far better to do it using constructors though.

Pointer to a member-function

I would like to do the following:
I have two classes, A and B, and want to bind a function from A to a function from B so that whenever something calls the function in B, the function from A is called.
So basically, this is the scenario:
(important A and B should be independent classes)
This would be class A:
class A {
private:
// some needed variables for "doStuff"
public:
void doStuff(int param1, float *param2);
}
This is class B
class B {
private:
void callTheFunction();
public:
void setTheFunction();
}
And this is how I would like to work with these classes:
B *b = new B();
A *a = new A();
b->setTheFunction(a->doStuff); // obviously not working :(
I've read that this could be achieved with std::function, how would this work? Also, does this have an impact in the performance whenever callTheFunction() is called? In my example, its a audio-callback function which should call the sample-generating function of another class.
Solution based on usage C++11 std::function and std::bind.
#include <functional>
#include <stdlib.h>
#include <iostream>
using functionType = std::function <void (int, float *)>;
class A
{
public:
void doStuff (int param1, float * param2)
{
std::cout << param1 << " " << (param2 ? * param2 : 0.0f) << std::endl;
};
};
class B
{
public:
void callTheFunction ()
{
function (i, f);
};
void setTheFunction (const functionType specificFunction)
{
function = specificFunction;
};
functionType function {};
int i {0};
float * f {nullptr};
};
int main (int argc, char * argv [])
{
using std::placeholders::_1;
using std::placeholders::_2;
A a;
B b;
b.setTheFunction (std::bind (& A::doStuff, & a, _1, _2) );
b.callTheFunction ();
b.i = 42;
b.f = new float {7.0f};
b.callTheFunction ();
delete b.f;
return EXIT_SUCCESS;
}
Compile:
$ g++ func.cpp -std=c++11 -o func
Output:
$ ./func
0 0
42 7
Here's a basic skeleton:
struct B
{
A * a_instance;
void (A::*a_method)(int, float *);
B() : a_instance(nullptr), a_method(nullptr) {}
void callTheFunction(int a, float * b)
{
if (a_instance && a_method)
{
(a_instance->*a_method)(a, b);
}
}
};
Usage:
A a;
B b;
b.a_instance = &a;
b.a_method = &A::doStuff;
b.callTheFunction(10, nullptr);
This i basic a solution
class A {
private:
// some needed variables for "doStuff"
public:
void doStuff(int param1, float *param2)
{
}
};
typedef void (A::*TMethodPtr)(int param1, float *param2);
class B {
private:
TMethodPtr m_pMethod;
A* m_Obj;
void callTheFunction()
{
float f;
(m_Obj->*m_pMethod)(10, &f);
}
public:
void setTheFunction(A* Obj, TMethodPtr pMethod)
{
m_pMethod = pMethod;
m_Obj = Obj;
}
};
void main()
{
B *b = new B();
A *a = new A();
b->setTheFunction(a, A::doStuff); // now work :)
}

Is there a way to forbid casting to subclass that is non-const in C++?

Here is a complete example.
I want to forbid using A::set from objects casted from B to A by allowing only casting
B to const A.
How to do it?
(I can't use virtual functions)
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { i_ = i; }
protected:
int i_;
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
void set(int i) { A::set(i); copy_i_ = i; }
protected:
int copy_i_;
};
void test2() {
A a;
a.set(3); // ok here
cout << a.get() << endl;
B b;
b.set(5);
A& aa = b;
assert(b.ok());
aa.set(3); // not ok here
assert(b.ok()); // fail-here
}
int main() {
test2();
return 0;
}
You could make the inheritance private and provide a member function in B to use instead of casting.
const A& B::convert_to_A() const { return *this; }
Why casting? Making void A::set(int i) protected will work in your case.
There is no need for forbidding non-const casts. You can solve your problem by using the template method design pattern.
#include "stdafx.h"
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { assert(i_ = i); copy_i();}
protected:
int i_;
virtual void copy_i(){};
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
protected:
int copy_i_;
void copy_i(){copy_i_ = i_; }
};
void test2() {
B b;
b.set(5);
A& a = b;
assert(b.ok());
a.set(3);
assert(b.ok()); // success!
}
int main() {
test2();
return 0;
}