sara
sara

Reputation: 95

can i access the variable declared in main in any function?

i have two files

 books.cpp and books.h

i am using these classes in my main function. i also have a function func in main.cpp file.

#include iostream
#include "books.h"
void func(int a, int b);
int main(){
    books book;
    func(5,6);
    return 0;
}

void func(int a, int b){
   //can i use the book object declared in main in this function?
}

i want the books class object book declared in main in the function how can i access it? can anyone help me please?

Upvotes: 0

Views: 5728

Answers (1)

LoztInSpace
LoztInSpace

Reputation: 5697

Not directly. You will have to pass it in to your function or make it global.

void func(int a, int b, books book)
{
    // use me
}

Upvotes: 3

Related Questions