Results 1 to 19 of 19

Thread: How to just declare class instance?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to just declare class instance?

    Hi everyone.

    Apologies, if this sounds a bit dumb, but I just can't figure it out....Suppose I want to declare an instance of a class without creating it i.e. calling it's constructor. Let's say I have a gui desktop app and would like to declare instance of class on some gui show up event and make it public, well actually visible to other methods inside same gui. How would I do that? I mean something like pseudo bellow:
    Qt Code:
    1. class Car {
    2. public:
    3. Car(int a, int b) {
    4. //some code
    5. }
    6. SetColor(int clr) {
    7. //some code
    8. }
    9. }
    10.  
    11. //code on mainwindow.cpp
    12. Car Fiat; //<-but just declar it, don't create it
    13.  
    14. void MainWindow::Method1(int start,int end) {
    15. Fiat...<- call constructor here
    16. }
    17.  
    18. void MainWindow::Method2(int color) {
    19. Fiat.SetColor(color);
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    You do that by instantiating the object on the heap, i.e. using the new operator

    https://en.wikipedia.org/wiki/New_(C++)

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by anda_skoa View Post
    You do that by instantiating the object on the heap, i.e. using the new operator

    https://en.wikipedia.org/wiki/New_(C++)

    Cheers,
    _
    Thanks, yes you are right So I should first declare a pointer to my class and then create the instance of object on the heap with new operator. Would this work:
    Qt Code:
    1. class Car {
    2. public:
    3. Car(int a, int b) {
    4. //some code
    5. }
    6. SetColor(int clr) {
    7. //some code
    8. }
    9. }
    10.  
    11. //code on mainwindow.cpp
    12. Car *Fiat;
    13.  
    14. void MainWindow::Method1(int start,int end) {
    15. Fiat = new Car(start,end);
    16. }
    17.  
    18. void MainWindow::Method2(int color) {
    19. Fiat->SetColor(color);
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to just declare class instance?

    Yes, but you you now have to manage the allocated object and deallocate the memory ( delete or use a smart pointer) when it is no longer needed.

    BTW. Should your Car* be a member variable of the MainWindow class?

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to just declare class instance?

    If all you want to do is make the class visible, but not instantiate it, then just forward declare it as below:

    Qt Code:
    1. class Car;
    To copy to clipboard, switch view to plain text mode 

    If any other code needs to know more than Car is a class, then you should include the header file for your Car class, etc.

  6. #6
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by ChrisW67 View Post
    Yes, but you you now have to manage the allocated object and deallocate the memory ( delete or use a smart pointer) when it is no longer needed.

    BTW. Should your Car* be a member variable of the MainWindow class?
    Ok thanks. Well yes, pointer *Fiat should be a member variable of MainWindow class in my case, I would not like it to be a local variable of a single method, but belonging to the let's say MainWindow class. I know there are other methods for gaining same result, passing by refernce, etc..This example is made up, but I just wanted to know how to do it. Thanks again.


    Added after 5 minutes:


    Quote Originally Posted by jefftee View Post
    If all you want to do is make the class visible, but not instantiate it, then just forward declare it as below:

    Qt Code:
    1. class Car;
    To copy to clipboard, switch view to plain text mode 
    If any other code needs to know more than Car is a class, then you should include the header file for your Car class, etc.
    Thanks jefftee, but I'm not sure this is right. Doing
    Qt Code:
    1. class Car;
    To copy to clipboard, switch view to plain text mode 
    will create and instance of Car by calling the default constructor with no parameters, just like
    Qt Code:
    1. class Car();
    To copy to clipboard, switch view to plain text mode 
    Last edited by arcull; 30th June 2015 at 05:58.

  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to just declare class instance?

    Quote Originally Posted by arcull View Post
    Thanks jefftee, but I'm not sure this is right. Doing
    Qt Code:
    1. class Car;
    To copy to clipboard, switch view to plain text mode 
    will create and instance of Car by calling the default constructor with no parameters, just like
    Qt Code:
    1. class Car();
    To copy to clipboard, switch view to plain text mode 
    Actually, it's a forward declaration and does not instantiate the class. Google it or read this for more info.

  8. #8
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by jefftee View Post
    Actually, it's a forward declaration and does not instantiate the class. Google it or read this for more info.
    I'm not sure about this. The constructor is called this way. Try the sample below and check output:

    car.h
    Qt Code:
    1. #ifndef CAR_H
    2. #define CAR_H
    3.  
    4. #include <iostream>
    5.  
    6. class Car
    7. {
    8. public:
    9. Car() {
    10. std::cout<<"default constructor\n";
    11. }
    12.  
    13. Car(int a,int b) {
    14. std::cout<<"constructor with 2 params\n";
    15. }
    16. };
    17.  
    18. #endif // CAR_H
    To copy to clipboard, switch view to plain text mode 
    car.cpp
    Qt Code:
    1. #include "car.h"
    2.  
    3. Car::Car()
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include<iostream>
    3. #include "car.h"
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9. std::cout<<"starting...\n";
    10.  
    11. Car Fiat;
    12. Car Audi;
    13.  
    14. std::cout<<"ending...\n";
    15.  
    16. return a.exec();
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to just declare class instance?

    Qt Code:
    1. class Car();
    To copy to clipboard, switch view to plain text mode 
    This should result in compilation error.

    Your code example does not contain forward declaration. Try this way:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include<iostream>
    3.  
    4. class Car; // forward declaration of a class Car
    5. // note that the "car.h" is not even included
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10. std::cout<<"starting...\n";
    11.  
    12. std::cout<<"ending...\n";
    13.  
    14. return a.exec();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    btw. how can you even compile and link your example ? you have multiple definitions of the Car class default constructor.
    Last edited by stampede; 30th June 2015 at 12:12. Reason: updated contents

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by arcull View Post
    I'm not sure about this.
    The comment regarding forward declaration was not relevant for your question on delayed instantiation.

    But you used "declare an instance" so some readers got the feeling you were asking about declaration without include

    Cheers,
    _

  11. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to just declare class instance?

    Quote Originally Posted by arcull View Post
    I'm not sure about this. The constructor is called this way. Try the sample below and check output:
    Your example doesn't forward declare anything. Doesn't matter though, as anda_skoa pointed out, I misunderstood what you were originally asking.
    Last edited by jefftee; 30th June 2015 at 16:37.

  12. #12
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by stampede View Post
    Qt Code:
    1. class Car();
    To copy to clipboard, switch view to plain text mode 
    This should result in compilation error.
    I guesss that depends on compiler, in my case it doesn't. But you are right, with the "()" you are making compiler think it is a function which returns a class. I've mixed up the declaration and definition in my Car class and you are right again it does not compile. Here is a corrected example:
    car.h
    Qt Code:
    1. #ifndef CAR_H
    2. #define CAR_H
    3.  
    4. #include <iostream>
    5.  
    6. class Car
    7. {
    8. public:
    9. Car();
    10. Car(int a, int b);
    11. };
    12.  
    13. #endif // CAR_H
    To copy to clipboard, switch view to plain text mode 
    car.cpp
    Qt Code:
    1. #include "car.h"
    2.  
    3. Car::Car()
    4. {
    5. std::cout<<"default constructor\n";
    6. }
    7.  
    8. Car::Car(int a, int b){
    9. std::cout<<"constructor with 2 params\n";
    10. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <iostream>
    3. #include <car.h>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. std::cout<<"starting...\n";
    10.  
    11. Car Fiat;
    12. Car Audi;
    13.  
    14. std::cout<<"ending...\n";
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    However what I want was already answered at the beginning, that is, create a pointer and crate instance later. My apologies for misleading question and thanks for your help.

  13. #13
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to just declare class instance?

    I guesss that depends on compiler, in my case it doesn't.
    I'm just curious, which compiler is it ?

  14. #14
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by stampede View Post
    I'm just curious, which compiler is it ?
    MinGW 4.8.2 32bit

  15. #15
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to just declare class instance?

    That's one of weirdest things I've seen recently, as it doesn't look anything like a function declaration, as "class" is a reserved keyword in C++.
    I tried to reproduce this with g++ 4.8.4, 5.0 and 6.0.0 (experimental build) but they always returned a compilation error. To me it is clearly a syntax error and I really would like to see the whole source code you've used when compiling this as well as warning messages generated with -Wall options. If you don't mind

  16. #16
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by stampede View Post
    That's one of weirdest things I've seen recently, as it doesn't look anything like a function declaration, as "class" is a reserved keyword in C++.
    I tried to reproduce this with g++ 4.8.4, 5.0 and 6.0.0 (experimental build) but they always returned a compilation error. To me it is clearly a syntax error and I really would like to see the whole source code you've used when compiling this as well as warning messages generated with -Wall options. If you don't mind
    Sorry my fault again, I was referring to second line, creation of "Audi" object .
    Qt Code:
    1. Car Fiat; //default constructor called
    2. Car Audi(); //default constructor not called
    3. class Car(); //compile error
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to just declare class instance?

    Ok, that makes sense

  18. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    Quote Originally Posted by arcull View Post
    Sorry my fault again, I was referring to second line, creation of "Audi" object .
    Qt Code:
    1. Car Fiat; //default constructor called
    2. Car Audi(); //default constructor not called
    3. class Car(); //compile error
    To copy to clipboard, switch view to plain text mode 
    Yeah, that's a tricky one, sometimes used in job interviews

    The Audi line is a prototype/forward declaration for a function called Audi which returns a Car object.

    Cheers,
    _

  19. #19
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to just declare class instance?

    On a side note, I suppose that the intention that the OP had when writing
    Qt Code:
    1. Car Audi(); // Actually a function declaration, as explained in this thread
    To copy to clipboard, switch view to plain text mode 
    was to perform value initialization, as in
    Qt Code:
    1. Car Audi{}; // Notice the use of the C++11 syntax, that avoids the "function declaration" problem
    2. pAudi = new Car();
    To copy to clipboard, switch view to plain text mode 
    , which is in general not the same as default initialization
    Qt Code:
    1. Car Audi;
    2. pAudi = new Car;
    To copy to clipboard, switch view to plain text mode 
    , although in the case of this particular class Car, both end up doing the same thing (calling Car::Car()).

Similar Threads

  1. Must I declare or not SHARED_EXPORT for an inner class ?
    By tonnot in forum General Programming
    Replies: 0
    Last Post: 14th February 2011, 12:30
  2. Replies: 1
    Last Post: 17th December 2010, 15:43
  3. How to declare global variable outside a class?
    By babygal in forum Qt Programming
    Replies: 2
    Last Post: 26th August 2010, 08:35
  4. Replies: 4
    Last Post: 13th May 2009, 17:50
  5. declare array in a class
    By mickey in forum General Programming
    Replies: 5
    Last Post: 17th April 2006, 20:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.