Hello!
I tried in Gamer's example to "preserve" an object created in a function in the A class using dynamic allocation, so that I can call it directly from main.
Please, can U help me finding basic mistakes? I can't google easy example where objects of the classes are memorised like usula objects (variables).
#include <iostream>
class B
{
friendclass A;
private:
B(int c) : i(c) {}
int i;
};
class A
{
public:
void a()
{
B b(10);
cout<<b.i<<endl;
b.i=b.i+2;
A * p= new A(b.i); //here I wanted to memorise the OBJECT using the
}
};
int main()
{
A a;
a.a();
cout<<p->i<<endl; //here I want to cout that exact memorised object's //attribute..
delete p;
}
line 20 declares a variable which is an A* but it is declared inside a(){} so when a() finishes p is destroyed. if you want p to exist after a() exits you need to declare it at class scope like B::i
also, when a() exits and destroys p, it does NOT destroy the A that was newed because it is on the heap. which leaves you with a memory leak.
Many thanks! Plese, before I lounch anohter stupid question, if someone has a good example link with class objects codes, please send me here! Thanks in advance!!!
#include <iostream>
class B
{
friendclass A;
private:
B(int c) : i(c) {}
int i;
};
class A
{
public:
A*p;
void a()
{
B b(10);
cout<<b.i<<endl;
// b.i=b.i+2;
p= new A(77); //here I wanted to memorise the OBJECT using the
}
~A(){delete p};
};
int main()
{
A a;
a.a();
cout<<a->i<<endl; //here I want to cout that exact memorised object's //attribute..
delete p;
}
t.cpp: In member function 'void A::a()':
Line 21: error: no matching function for call to 'A::A(int)'
#include <iostream>
class B
{
friendclass A;
private:
B(int c) : i(c) {}
int i;
};
class A
{
public:
void a()
{
B b(10);
cout<<b.i<<endl;
A(int z): b.i(z} {};
//b.i=b.i+2;
p= new A(77); //here I wanted to memorise the OBJECT using the
}
A *p;
~A(){delete p};
};
int main()
{
A a;
a.a();
cout<<a->i<<endl; //here I want to cout that exact memorised object's //attribute..
delete p;
}
#include <iostream>
usingnamespace std;
class B
{
friendclass A;
private:
B(int c) : i(c) {}
int i;
};
class A
{
public:
void a()
{
B b(10);
cout<<b.i<<endl;
b.i=b.i+2;
i = b.i; //here I wanted to memorise the OBJECT using the
}
int i;
};
int main()
{
A a;
a.a();
cout<<a.i<<endl; //here I want to cout that exact memorised object's //attribute..
}
#include <iostream>
usingnamespace std;
class B
{
friendclass A;
private:
B(int c) : i(c) {}
int i;
};
class A
{
public:
A*p;
void a()
{
B b(10);
cout<<b.i<<endl;
// b.i=b.i+2;
p= new A; //here I wanted to memorise the OBJECT WITHIN the class /function...
*p=B b(5);
}
~A(){delete p;};
};
int main()
{
A a;
a.a();
cout<<p->a()<<endl; //here I want to cout that exact memorised object's //attribute..
}
#include <iostream>
usingnamespace std;
class B
{
friendclass A;
private:
B(int c) : i(c) {}
int i;
};
class A
{
public:
B *p;
B b(10);
cout<<b.i<<endl;
// b.i=b.i+2;
A(){p=new B(5)}
//here I wanted to memorise the OBJECT WITHIN the class /function...
};
~A(){delete p;};
};
int main()
{
A a;
a.a();
cout<<p->a()<<endl; //here I want to cout that exact memorised object's //attribute..
}
void a()
{
B b(10);
cout<<b.i<<endl;
b.i=b.i+2;
p= new B(b.i); //save a copy of b for later
}
also, your latest code @34 still attempts to reference p which is inside a. you must say a.p to get that p. even then you wont be able cout it because only class A is a friend of class B, so only class A can reach inside to get at i.