0

Yes I have read a lot of tutorials and questions and also tried a lot of combinations but it seems not to work. My goal is not to use dynamic allocation.

My classes look like this:

Pages
Page
PMain:Page
PCam:Page

on my main when I do this:

1. main:

Page * page;
PCam main;
main.setContext(context);
page = &main;
page->echo();

result: PCam

but when I try to create the instance inside an outside class and point it to page it fails.

2.

pages class:

Pages::Pages(Page*& page, Context& context){
    this->context = &context;
    PMain main;
    main.setContext(*this->context);
    main.echo();
    // page = &main; <---
} 

main:

Page * page;
Pages pages(page, context);
page->echo();

result: Page

expected result: PCam

My classes:

Page:

void Page::setContext(Context & context)
{
    this->context = &context;
}
void Page::echo()  //virtual
{
    std::cout << "echo Page"  << std::endl;
}

PMain:

void PMain::echo(){
    std::cout << "echo PMain"  << std::endl;}
}

PCam:

void PCam::echo(){
    std::cout << "echo PCam"  << std::endl;}
}

Any help would be appreciated. thanks.

dragosht
  • 3,237
  • 2
  • 23
  • 32
jico
  • 3
  • 1
  • 4
  • 1
    please indent your code. – thebjorn Jul 20 '14 at 18:39
  • "but when I try to create the instance inside an outside class and point it to page it fails." - What does that mean? Are the 1. and 2. separate questions? – Joseph Mansfield Jul 20 '14 at 18:40
  • 1
    *How* do your code fail? Do you get errors building? Do you get crashes? If you get build errors, please edit your question to include them. If you get crashes, run your program in a debugger. – Some programmer dude Jul 20 '14 at 18:40
  • 1
    Because `main` is destroyed when it goes out of scope (when the constructor finishes and exits). Any attempt to access it after it's been destroyed results in undefined behavior. – Captain Obvlious Jul 20 '14 at 18:41
  • 3
    You are creating a local variable **main** in the constructor of _Pages_ that would be destructed when leave constructor scope, and you are assigning his address to a pointer and after the constructor end, this pointer would be pointing to free memory. – NetVipeC Jul 20 '14 at 18:42
  • 1
    Please post actual, compilable code and not random snippets. It *seems* like `Pages::Pages()` is storing the address of a local variable into the pointer passed. – T.C. Jul 20 '14 at 18:42
  • 2
    `PCam main;` its recommended to never use the name `main` for anything but program entry point – sp2danny Jul 20 '14 at 18:44
  • Out of interest, the line in the constructor `// page = &main; <---` why did you comment that out and what happens if you don't? Also, you really need to post all relevant class declarations and definitions if you expect people to help. – Component 10 Jul 20 '14 at 18:50
  • all other deceleration is headers, And that page=&main is pointless because main in null when the class is finished. I thought that there is another way to do this. – jico Jul 20 '14 at 19:19

1 Answers1

1

Your problem, or one of them, is that this:

Pages::Pages(Page*& page, Context& context){
    [...]
    PMain main;

is a local stack variable. When this function returns, it ceases to exist. If you've assigned it to a pointer, you'll get undefined behavior by using it.

My goal is not to use dynamic allocation.

Unless you have some specific reason, this is a mostly pointless goal. If you want a pointer to a stack object (i.e., one that's not dynamically allocated), that object must remain in scope as long as you use the pointer. If you can't do that, then you need to put it on the heap (i.e., dynamically allocate).

CodeClown42
  • 11,194
  • 1
  • 32
  • 67