Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Forward declaration of class QColor when pass by Value of Object of QColor

Forward declaration of class QColor when pass by Value of Object of QColor

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 831 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    Hi All
    I have file is -------- myModel.h

    class QColor; --------------- forward declaration

    class myModel : public QAbtrsatrcItemModel {
    protected :
    QColor getColor()
    void setColor(QModelIndex& , QColor);
    };

    file is myModel.cc
    #include "myModel.h"
    #include <QColor>

    QColor myModel:: getColor(QModelIndex& index) {
    ............
    ....
    }
    void myModel:setColor(QModelndex&,QColor) {
    ...
    ...
    }

    As I understand the forward declaration of a class only works when we are using pointer to the class object but in this case we are using QColor as pass by value and return by Value , but my code is compliling and working .
    Can someone explain me how ?

    raven-worxR 1 Reply Last reply
    0
    • Q Qt Enthusiast

      Hi All
      I have file is -------- myModel.h

      class QColor; --------------- forward declaration

      class myModel : public QAbtrsatrcItemModel {
      protected :
      QColor getColor()
      void setColor(QModelIndex& , QColor);
      };

      file is myModel.cc
      #include "myModel.h"
      #include <QColor>

      QColor myModel:: getColor(QModelIndex& index) {
      ............
      ....
      }
      void myModel:setColor(QModelndex&,QColor) {
      ...
      ...
      }

      As I understand the forward declaration of a class only works when we are using pointer to the class object but in this case we are using QColor as pass by value and return by Value , but my code is compliling and working .
      Can someone explain me how ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Qt-Enthusiast
      because in your case it's enough for the compiler to know that the type exists. Since it's not accessing any members of it.
      When you would move the implementation of the getter and setter to your header file the compiler would need to know more about the class and thus a simple forward declaration wouldn't be sufficient.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        So I conclude this code is fine . Please let me know your feedback

        raven-worxR 1 Reply Last reply
        0
        • Q Qt Enthusiast

          So I conclude this code is fine . Please let me know your feedback

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Qt-Enthusiast said:

          So I conclude this code is fine . Please let me know your feedback

          sure. it compiles ;)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved