Creating an object of a class inside another class - possible?

This is a discussion on Creating an object of a class inside another class - possible? within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to create an object of a class inside a member function of another class...possible? I am doing ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Creating an object of a class inside another class - possible?

    Hello,
    I want to create an object of a class inside a member function of another class...possible?
    I am doing QT GUI programming, and am trying to connect two dialogs together, through the use of a function that is called when the user clicks the "Next" button. It would seem as if I would also need a way to hide the first dialog when the second dialog is called.

    I have experimented with different methods, but so far none of them work.

    Help?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Yes, that is possible. You can create a class object anywhere as long as its constructor is public.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by bithub View Post
    Yes, that is possible. You can create a class object anywhere as long as its constructor is public.
    Yes, the constructor is public.
    Thanks.

    Next question:

    I have tried to create a class object inside a member function (of the first dialog class), but keep getting compiler errors. The error I'm getting now suggests that a private slot inside the second dialog's class is not getting called for some reason, because it gives me a message something like

    "Something forbids declaration of "QLineEdit" with no type"

    or something to that effect (I'm don't remember the exact error message). Now the line its referring to looks like the following in the second dialog's class:

    Code:
    QLineEdit* lineEdit;
    As you can see, its a pointer to type "QLineEdit" (a special QT class). Now, it shouldn't be giving me that error message seeing as its a type included in a QT header file that included, and so it indicates that its not getting to the code I'm using to use "lineEdit". Now, I figure the reason is the second dialog's class is not being "built" until the nextClicked() function (which is where I created the object of the second dialog's class) is called, which makes sense. In other words, what I'm trying to say here, is, the second dialog's members must not be actually created until the Next button is clicked, and for some reason, the compiler doesn't like this (and I don't know why).

    The reason I think that is the case, is because I have tried creating the object of the second dialog's class inside int main(), and the compiler does not give me the error I mentioned above...it only appears when I try to create the object of the class inside the member function of the other class.

    Anyway, I can probably figure that one out myself, but thought I would just go ahead and mention it, in case anyone here has any thoughts on the matter.

    Another problem is:

    Code:
    TextToSpeechDialog1* dialog = new TextToSpeechDialog1;
    dialog->show();
    This code works fine, and causes the first class's (TextToSpeechDialog1) dialog to be shown.
    However, in order to hide the first dialog when the second dialog is called, I tried to do this
    Code:
    void TextToSpeechDialog1::nextClicked() {
    TextToSpeechDialog2* dialog2 = new TextToSpeechDialog2;
    dialog->close(); //close() is a function that hides QWidgets from view, but does not delete
    dialog2->show();
    }
    But the compiler will give me a message saying "dialog" was not declared in that scope. So how do I get around this, and hide "dialog" (the first dialog's class's object) from inside dialog itself?

    Is this even possible? I guess its recursion, but I don't know how to work it.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    To the error message: It is likely that the compiler cannot find the declaration of the class, hence it complains that you cannot create a pointer to an unknown type. You should check your files and includes.

    As for the second: You know about scopes, right? Something created in one function cannot be seen in another. If you put it as a member variable of a class, all of the class's members can access it.

    And thirdly: Is there a reason you are using dynamic memory on all those objects? From what I see, you have lots of leaks. You do realize you can create them on the stack, right?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    To the error message: It is likely that the compiler cannot find the declaration of the class, hence it complains that you cannot create a pointer to an unknown type. You should check your files and includes.
    Actually, the class is definitely declared (in the header file TTS_mod.h, which I then included into TTS_mod.cpp), and I wrote the code myself. As I mentioned before, the error only comes when I try to create an object of class "TextToSpeechDialog2" inside a member function of "TextToSpeechDialog1". (The member function is nextClicked() ) The error does not come when I create the object of the class in int main().
    As for the second: You know about scopes, right? Something created in one function cannot be seen in another. If you put it as a member variable of a class, all of the class's members can access it.
    Duh! Right, I forgot about that. Would I be able to use a pointer, then, to point to the object "dialog" in int main() from inside the member function of TextToSpeechDialog1, and then use the pointer to modify it? Would recursion work that way?
    And thirdly: Is there a reason you are using dynamic memory on all those objects? From what I see, you have lots of leaks. You do realize you can create them on the stack, right?
    There are no leaks, because TextToSpeechDialog1 and TextToSpeechDialog2 are actually derived classes of the QT class "QDialog", which handles deletion of its own members.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    dialog would be declared in the scope if it were a global or a member of TextToSpeechDialog1. The first snippet

    Code:
    TextToSpeechDialog1* dialog = new TextToSpeechDialog1;
    dialog->show();
    seems to indicate that this is not the case (or your code is rather bizarre).

    (I have a feeling that closing one dialog and showing another shouldn't be done by the first dialog, but by something that knows about both dialogs - and holds a pointer to dialog as I suspect is the case here.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs up

    Quote Originally Posted by anon View Post
    dialog would be declared in the scope if it were a global or a member of TextToSpeechDialog1. The first snippet

    Code:
    TextToSpeechDialog1* dialog = new TextToSpeechDialog1;
    dialog->show();
    seems to indicate that this is not the case (or your code is rather bizarre).
    Well, its neither. dialog is a object of class TextToSpeechDialog1, created in int main(). So its not global, and its not a member of TextToSpeechDialog1 either, because its an object (or at least from what I understand about objects).
    (I have a feeling that closing one dialog and showing another shouldn't be done by the first dialog, but by something that knows about both dialogs - and holds a pointer to dialog as I suspect is the case here.)
    Hmm...you could be right. That may be best in this case. Good tip.
    But the whole point of this exercise is to only hide the first dialog when the Next button of that dialog is clicked (which calls the nextClicked() function), and that can only be achieved in a member function of TextToSpeechDialog1 itself...unless I create a pointer to an outside (global) function from nextClicked(), if that's even possible. And then I could call the function from inside nextClicked. Then once inside the global function, I could use another pointer to modify the object. Yeah, that should work.
    Last edited by Programmer_P; 08-13-2009 at 02:25 PM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Quote Originally Posted by Programmer_P View Post
    There are no leaks, because TextToSpeechDialog1 and TextToSpeechDialog2 are actually derived classes of the QT class "QDialog", which handles deletion of its own members.
    Ugh. Another of these delete this or manual cleanup systems. I hate those.
    Meh. Anyway, carry on then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Ugh. Another of these delete this or manual cleanup systems. I hate those.
    Meh. Anyway, carry on then.
    Why? What's wrong with them?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    1) Delete this prevents objects from being created on the stack.
    2) It leaves dangling pointers.
    3) By default, we delete everything we new. But if it deletes itself, then we cannot delete it and so it looks to everyone but those who knows it deletes itself, that it's a memory leak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Elysia View Post
    1) Delete this prevents objects from being created on the stack.
    2) It leaves dangling pointers.
    3) By default, we delete everything we new. But if it deletes itself, then we cannot delete it and so it looks to everyone but those who knows it deletes itself, that it's a memory leak.
    To be fair, most frameworks which utilize this approach use factories to create objects. This means you don't have to worry about users calling "new" or creating an object on the stack.

    I have no idea how QT is implemented though.
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    If an implementation decides to create objects for you, I'd return them in a no_delete wrapper to protect against mistakes of deleting (plus returning a no_delete type pretty much screams: DON'T DELETE ME!).
    I'd rather just return a smart pointer from the factory, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by bithub View Post
    To be fair, most frameworks which utilize this approach use factories to create objects. This means you don't have to worry about users calling "new" or creating an object on the stack.

    I have no idea how QT is implemented though.
    Well, I have no idea how QT handles deletion of its objects either, but what do you mean by "factories"? I hand-coded the objects (as well as the classes) myself, following an example in the QT programming book C++ GUI Programming with QT 4.

    Anyway, I got it to work! I indeed had to call a global function (nextDialog())from inside nextClicked(), where I then simply created a new object of TextToSpeechDialog1, and then used that object to call the hide() function which hides the first dialog. I suppose I could have also put the object of TextToSpeechDialog2 inside the global function nextDialog() as well, but I instead put it directly inside nextClicked(), and it works.
    Last edited by Programmer_P; 08-14-2009 at 02:18 PM.

  14. #14
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    QObject::connect(&nextButton, SIGNAL(clicked()), &textToSpeechDialog1, SLOT(show()));
    ?

    Prefferably best to use Qt Designer, you can "Promote" whatever manual coded Qt widgets.

    About the factory that Elysia mentioned is boost library or std whose smart pointer to handle allocation and deallocation easily.

    Qt has one too called QPointer, however it's SLOWER than any other do.

    EDIT:
    It's QSharedPointer not QPointer :-P sorry.
    Last edited by audinue; 08-15-2009 at 03:11 AM.
    Just GET it OFF out my mind!!

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Quote Originally Posted by audinue View Post
    About the factory that Elysia mentioned is boost library or std whose smart pointer to handle allocation and deallocation easily.
    Factory is a C++ design.
    Basically, it's a class or object which creates other objects. COM is an example of implementation of this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. array of class object
    By TomButcher in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:48 AM
  4. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 03:52 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21