How can I access test.a from main?
Here is my code:
myfile1.cpp:
#include "myfile2.h"
int main()
{
test.a=1; //this gives error "incomplete type is not allowed"
}
myfile2.h:
class abc;
abc test;
myfile2.cpp:
#include "myfile2.h"
class abc{
public:
int a;
abc():
a(0){}
} test;
You cannot define a variable of incomplete type, but you can declare one. If you don't want to make the class definition public, then you cannot access class members outside the translation unit where you define the class, so you also need to provide accessors. Here's a possible approach:
header.h:
class abc; // declares the name "abc"
extern abc test; // declares the name "test"
void set_a(abc & obj, int val); // declares the name "set_a"
impl.cpp:
#include "header.h"
class abc { /* definition */ };
abc test;
void set_a(abc & obj, int val) { obj.a = val; }
main.cpp:
#include "header.h"
int main()
{
set_a(test, 1);
}
Related
I have the following code:
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
class A {
public:
int f();
int (A::*x)();
};
int A::f() {
return 1;
}
int main() {
A a;
a.x = &A::f;
printf("%d\n",(a.*(a.x))());
}
Where I can initialize the function pointer correctly. But I want to make the function pointer as static, I want to maintain single copy of this across all objects of this class.
When I declare it as static
class A {
public:
int f();
static int (A::*x)();
};
I am unsure of the way/syntax to initialize it to function f. Any resource would be helpful
A static pointer-to-member-function (I guess you already know this is different from a pointer to a static member function) is a kind of static member data, so you have to provide a definition outside the class like you would do with other static member data.
class A
{
public:
int f();
static int (A::*x)();
};
// readable version
using ptr_to_A_memfn = int (A::*)(void);
ptr_to_A_memfn A::x = &A::f;
// single-line version
int (A::* A::x)(void) = &A::f;
int main()
{
A a;
printf("%d\n",(a.*(A::x))());
}
I'm writing a class which is wrapped in a namespace named cs. I am using a library whose one of the function takes a function pointer. The function have to modify some of the protected members of the class so I wrote a free function in the same cs namespace and made it a friend function of the class. Doing that made it available for the clients to use that function. But The function MUST be inaccessible from the client due to obvious reasons.
An example code is here:
#include "lib.h"
namespace cs{
class A
{
protected:
int x;
float y;
friend int myFunc(void* userdata, int valInt, float valFloat);
public:
void abc()
{
libFunc(this, myFunc);
}
};
void myFunc(void *userdata, int x, float y){
// I would like this function to be inaccessible from the client
A *obj = (A*) userdata;
obj->x = x;
obj->y = y;
}
}
If you want to make a free function inaccessible in another compilation unit, you may use a nested anonymous namespace:
namespace cs{
class A
{
protected:
//...
friend int myFunc(int valInt, float valFloat);
public:
void abc();
};
namespace { // anonymous nested namespace
int myFunc(int x, float y){
...
}
}
void A:: abc() {
libFunc(this, myFunc);
}
}
Header.h
#pragma once
namespace
{
class B;
}
namespace n1
{
namespace n2
{
class A
{
private:
int i;
public:
friend class B;
};
}
}
Source.cpp
#include <stdio.h>
#include "Header.h"
class B
{
public:
void Run();
};
void B::Run()
{
n1::n2::A a;
a.i;
}
int main()
{
B b;
b.Run();
}
As we can see from above Class A is defined in header file while class B is defined in source file. I want to access private member of Class A from Class B::run(). I am not able to find the way to do this.
you are forward declaring class B in anonymous namespace
take out class B forward declaration out of the namespace and it should work
like this:
#pragma once
class B;
namespace n1
{
namespace n2
{
class A
{
private:
int i;
public:
friend class B;
};
}
}
I have a class with a private static variable. The main function should change the value in the variable, but even if I set the main function as a friend of the class the compiler tells me that the variable is private and not accessible from main.
Example:
ClassA.h:
namespace nameA{
class ClassA {
private:
static int varA;
public:
ClassA(){};
friend int main(void);
};
}
ClassA.cpp:
namespace nameA{
int ClassA::varA = 0;
}
main:
int main(void){
ClassA::varA = 42; //ERROR
}
I don't know if "friend" also allows access to static members or if I have to find another solution.
It because friend function main in ClassA is located in nameA namespace.
If you want to declare as friend the int main(void) function, that located in global scope, you should do it this way:
friend int ::main(void);
Whole source (compiled in VS2015):
int main(void);
namespace nameA {
class ClassA {
private:
static int varA;
public:
ClassA() {};
friend int ::main(void);
};
}
namespace nameA {
int ClassA::varA = 0;
}
int main(void) {
nameA::ClassA::varA = 42;
return 0;
}
Your friend declarations grants friendship to a function named main in the namespace nameA, not the global main function.
Your code is equivalent to
namespace nameA
{
int main(void);
class classA
{
...
friend int main(void);
};
}
You need to declare main before the namespace starts.
int main(void);
namespace nameA
{
class classA
{
...
friend int main(void);
};
}
should work
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;
}