from main to another function

Hello!
This code works.
Please, I would move the dynamically allocated object to any function other then main.
Please, any help?
Many thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 // Example program
#include <iostream>
using namespace std;

class First{
    public:
    int i;
    
    First(int z) : i(z) {};
};

int main(){
    First f(8);
    cout<<f.i<<endl;
    
    First*po;
    po=new First(9);
    
    
    cout<<po->i<<endl;
    delete po;
    
    
    return 0;
}


Please, do I need destructor for that?
Last edited on
>Please, I would move the dynamically allocated object to any function other then main.

What do you really want?
What do you mean by "move"?

If you mean pass it to a function just pass it as a pointer- or dereference it.
No, I mean opposite: in THIS example, I made dynamically allocated OBJECT of the type class First IN THE FUNCTION MAIN.

I wonder, if I can make object of the class First IN SOME OTHER FUNCTION, out of main, and , as it is dynamically allocated, to call it FROM MAIN.
I will RESPOND IN KIND :D

You most CERTAINLY CAN (this is fun) have you TRIED IT?

IN ORDER to DO this YOU will HAVE to (lol) use the EXACT same SYNTAX you USED HERE. (I'm crying)

Just be careful about memory leaks, if you dynamically allocate the memory be sure to delete IT!

(hahahaha these caps)
Ok, please, tell me someone how to call it from main before all, please? I cant google example that does exactly this what I want..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   
class First{
  public:
   int i;
    
    
   
    First(int z) : i(z) {};
    void makeobject(){  /7function returns nothing, just"WORKS"
        First* fobj2;
        fobj2=new First(11);
        delete fobj2;
    //return fobj2.i;  //i do not want the function to send it...
    }
        
    
};

int main(){
    First f(8);
    cout<<f.i<<endl;
    
    First*po;   //let's say this is ok, let it for later...
    po=new First(7);
    
    cout<<f.i<<endl;
    cout<<po->i<<endl;
    delete po;
    
    cout<<?
    
    return 0;
}
   


Last edited on
I want to call from main right that object whose i value is 11 and whose name is fobj2...


cout<<fobj2.i<<endl is obviously not the solution, so it can't be the same way...


cout<<fobj2->i<<endl;
cout<<obj2->makeobject()<<endl; -> sure, wrong both, so?
Last edited on
Please help someone, can't google anything similar...
I'm not understanding the question.

Lines 9-14 essentially accomplish nothing. You create a pointer to an object of First (fobj2). fobj2.i is initialized to 11, then you delete fobj2. The object and the value 11 are gone.

What is it that you're trying to do?

cout<<fobj2->i<<endl;

That depends where you put it. If you put that cout after line 11, you're fine. You can't do that at line 30, because fobj2 no longer exists.

cout<<obj2->makeobject()<<endl; -> sure, wrong both, so?


You have nothing called obj2, so that won't work. Did you mean fobj2?
If so, that won;t work because fobj2 no longer exists.

If you're trying to print the value of fobj2.i at line 30, then changing makeobject as follows will work:

1
2
3
4
5
6
7
8
int makeobject()
{    First* fobj2;
    int temp;
    fobj2=new First(11);
    temp = fobj2->i;
    delete fobj2;
    return temp; 
}

Then at line 30:
 
cout<< f.makeobject() <<endl; // Will print 11 without changing f.i 







Hello!
Yes I unerstand what U ment, but I ment sth similar, not hte same exactly.

Please, why is fobj2 not declared in scope main now?
Did I not write its declaration on the right place now?

many thanks!!!
Please SOS!!!

http://ideone.com/o06oQM
I ment sth similar, not hte same exactly.

I don't understand what you're saying.

why is fobj2 not declared in scope main now?

Because it is a local pointer within makeobject. fobj2 goes out of scope when makeobject exits.

Did I not write its declaration on the right place now?

Since you haven't explained what you're trying to do, I don't know what to recommend.
Do you want the fobj2 pointer to persist?

Your snippet on ideone changes the fobj2 pointer to a public member of first.
Lines 53-54 are incorrect. fobj2 is a member of First. Therefore you have to reference it through an instance of First. e.g.
53
54
55
  cout<<f.fobj2->i<<endl;
 
  delete f.fobj2;




hello! Here we are:
if :

Because it is a local pointer within makeobject. fobj2 goes out of scope when makeobject exits.

Why did we use dynamic allocation then?
Why did we use dynamic allocation then?

What's with "we"? :)

I'm just commenting on the code you posted. You still haven't explained what you're trying to accomplish.
Topic archived. No new replies allowed.