making auto_ptr to a friend class - c++

This is a sample code of my project.
I have to make the std::auto_ptr to a friend class, so that it can access private members.
#include "stdafx.h"
#include <map>
#include <iostream>
//sample namespace
namespace test
{
//class A
class A
{
public:
//making class B to friend , so that it can access private members
friend class B;
private:
int i;
//constructor - private
A(int in)
{
i = in;
}
// private destructor;
~A()
{
std::cout<<"\n Ending";
getchar();
}
};
//map to store A pointer
typedef std::map<int, std::auto_ptr<A>> MAP;
//class B, friend of A
class B
{
private:
MAP Map;
public:
//making auto_ptr to a friend class , so that it can call the destruct all the A pointer.
friend class std::auto_ptr; //Getting error like" error C2990: 'std::auto_ptr'
//: non-class template has already been declared as a class template
B()
{
std::auto_ptr<A> a(new A(1));
std::auto_ptr<A> b(new A(2));
std::auto_ptr<A> c(new A(3));
Map[0] = a;
Map[1] = b;
Map[2] = c;
}
~B()
{
}
};
}
int _tmain(int argc, _TCHAR* argv[])
{
using namespace test;
B ab;
return 0;
}
But when i tried to make it to a friend i am getting error... please help....

Because auto_ptr is a template class, you'll need something like:
friend std::auto_ptr<B>;

Related

C++ Change private member of a class through only one other specific class?

How do I change the int 'a' inside class A but restrict the function that changes the int to ONLY class B? I do not even want A to be able to change it.
I have tried to create a friend function, but I get an error I included in the comments of the code. I then tried to just forward declare the function as a friend function in private and then actually creating the function in public, but the C class can access the public function. This despite me writing (in the private section) that the function is a friend function. Can I 'friend' a private value? I am unsure what to do here.
#include <stdio.h>
#include <iostream>
int main() {
// forward declaration
class B;
class C;
class A {
private:
int a; // private member I want to edit, but only via class B.
int a2; // I would like to NOT be able to access this in class B. I only want to access and modify int a from class B, no other variable. If possible.
// invalid use of non-static data member 'a'
friend void test(int new_value) {
a = 5;
}
friend B;
public:
};
class B {
private:
int b;
public:
change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be possible.
}
};
class C {
private:
int c;
public:
change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be impossible
}
};
return 0;
}
Add "void" before prototypes of change_a_value, and change the
friend B
to
friend class B
and suppress friend in
friend void test(int new_value) {
complete corrected program :
int main() {
// forward declaration
class B;
class C;
class A {
private:
int a; // private member I want to edit, but only via class B.
int a2; // I would like to NOT be able to access this in class B. I only want to access and modify int a from class B, no other variable. If possible.
void test(int new_value) {
a = new_value;
}
friend class B;
public:
};
class B {
private:
int b;
public:
void change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be possible.
}
};
class C {
private:
int c;
public:
void change_a_value(A a_class, int new_int) {
a_class.test(new_int); // I want this to be impossible
}
};
return 0;
}
Compilation results are what was expected :
TestCpp2.cpp:9:14: error: ‘void main()::A::test(int)’ is private
void test(int new_value) {
^
TestCpp2.cpp:28:31: error: within this context
a_class.test(new_int); // I want this to be impossible
^
Makefile:510: recipe for target 'TestCpp2.o' failed
make: *** [TestCpp2.o] Error 1

Access class' std::vector with custum class object in a for loop

I have a classes like so:
#include "B.h"
class A{
public:
std::vector<B> f[32];
}
#include "C.h"
class B{
public:
C thing;
}
class C{
public:
void func(arg1);
}
And I try to do this in main:
int main(){
B test;
for(int i = 0; i < 32; i++){
test.f[i]->thing.func(arg1);}
}
but it doesn't let me. I get an error on "test" object saying expression must have pointer type. What does this mean? How do I fix it?
Vector size is not set using subscript operator, you might want to use vector constructor to achieve what you are trying to do. Here is modified version of your code :
#include <iostream>
#include <vector>
using namespace std;
class C{
public:
void func(int arg1) {}
};
class B{
public:
C thing;
};
class A{
public:
std::vector<B> f;
A(): f(std::vector<B>(32)) {}
};
int main() {
A test;
for(int i = 0; i < 32; i++){
test.f[i].thing.func(i);}
return 0;
}

Can i make a define constructor which contains an object from another class?

Can I do an defined constructor which contains an object from another class?
If i can do how is defined.
this is an example.I do classes and how could be defined the constructor of class "Abonati" which contains an object "abonament"
I need that because i have another class which contain a vector of "abonati"
#pragma once
#include"abonament.h"
#include<iostream>
#include<vector>
using namespace std;
class abonati
{
char*nume_abonat;
int nr_telefon;
char *numefisier;
abonament *a;
public:
abonati();
abonati(char*, int , char *,abonament *);
abonati(abonati&a);
void Send();
~abonati();
};
`#pragma once
#include"abonati.h"
class abonament
{
protected:
int cost;
public:
abonament();
abonament(int costa);
virtual ~abonament();
};
#include "abonament.h"
abonament::abonament()
{
this->cost = 0;
}
abonament::abonament(int costa)
{
this->cost = costa;
}
abonament::~abonament()
{
}
`
I guess you would like to pass an class instance to another class constructor
Here is an example
#include <iostream>
class A
{
public:
A(int value) : m_int(value) {}
int GetInt() { return m_int; }
private:
int m_int;
};
class B
{
public:
B(A& a) : m_int(a.GetInt()) {} // Here constructor expects instance of class A
int GetInt() { return m_int; }
private:
int m_int;
};
int main()
{
A a(2);
B b(a); // Pass an object of class A to constructor of class B
std::cout << b.GetInt() << std::endl;
return 0;
}
Prints
2

Map referencing class used by same class

I have a class that needs to keep a list of references of all its objects.
For example:
//A.cpp
class A {
A() {}
someMethod() {}
someOtherMethod() { mapA[0]->someMethod(); }
}
//main.cpp
#include <map>
std::map<int, A*> mapA;
int main(int argc, char const *argv[]) {
int count = 0;
A* a = new A();
mapA[count] = a;
count++;
}
However, because mapA is only global to main.cpp, A.cpp can't reference it. I tried using extern, but because the map uses the same class A, I don't know where to put it.
What's the best way to go about this?
You might register them in constructor, and make the static var in the class:
// a.hpp
class A {
public:
A() { as.insert(this); }
A(const A& rhs) { as.insert(this); }
~A() { as.erase(this); }
static std::set<A*> as; // Declaration
};
// a.cpp
std::set<A*> A::as; // Definition

Class Access Variable

I've written out a template of what the basic code I'm trying to figure out is based on so you don't need to sift through it
#include <iostream>
using namespace std;
class class1 {
int main()
{
return 0;
}
If you need to do it the the exact declaration order in your post you can do this with a forward declaration. This will allow you to declare accessclass2 to take an instance of class2 as an argument without knowing the details. You will simply need to define accessclass2 after class2 has been defined.
#include <iostream>
using namespace std;
// Forward declaration allows you to pass a reference to the function withing
// having the details of the class2 yet.
class class2;
class class1
{
public:
int x;
class1()
{
x = 0;
}
void accessclass2(class2& other);
};
class class2 {
public:
int y;
class2()
{
y = 10;
}
void print()
{
cout << y << endl;
}
};
void class1::accessclass2(class2& other)
{
other.print();
}
int main()
{
class1 foo;
class2 bar;
foo.accessclass2(bar);
return 0;
}