Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to initialize class with pointer to class



  • How to initialize class with pointer to class where object to is located;

    For example Inside class A is created class B object, now class A can com to class B via object but for class B to com to class A would it be possible to initialize class with pointer or there are better solutions?

    ´´´
    class FileCOM : public QObject
    {

    public:
    FileCOM(TCP_Socketptr); <<<------ this attempt fails and compiler returns : error: expected ')' before '' token

    void set_file_location();
    void read_file_to_buffer();
    void process_buffer();
    void write_file(QByteArray data);
    std::vector <QByteArray> read_buffer;
    

    };
    ´´´


  • Lifetime Qt Champion

    just create a constructor that accepts such pointer ?

    class FileCOM : public QObject
    {
    TCP_Socket *mykeptpointer;
    public:
    FileCOM(TCP_Socket * theptr) : mykeptpointer(theptr) {}
    ...
    or do i misunderstand you ?



  • Compiler returns this error msg : error: expected ')' before ' * ' token


  • Lifetime Qt Champion

    @mrjj said:

    are u sure it knows TCP_Socket ?

    you did include the .h file where it lives ?



  • Yes included , problem was due to double include in 2 files.
    Is it possible to include file only for .h inside use so when other files include same .h part of files would not be double included?


  • Lifetime Qt Champion

    This post is deleted!

  • Lifetime Qt Champion

    @Q139
    it is normal to include .h many times.

    Are you using guards ?

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #endif



  • For example when include file1.h in file2.h and file2.h in file1.h compiler returns error: expected ')' before '' token FileCOM(TCP_SocketSOCKETptr); or mostly error: 'FileCOM' does not name a type

    For pointer of class A in class B it need include class A .h in B .h file, but for class A to create object of B in .h file it need include of B .h file


  • Lifetime Qt Champion

    @Q139

    Ah, that is called circular include and often solved by using type forwards.
    https://en.wikipedia.org/wiki/Circular_dependency

    like
    in your case
    class TCP_Socket; //forward declaration

    class FileCOM : public QObject
    {
    TCP_Socket *mykeptpointer;
    ....

    and then DONT include its .h file



  • Then it is not possible to declare class FileCOM in .h file of TCP_Socket only .cpp file
    Thanks for your time and help, problem is there are multiple instances of sockets and each one should have fileCOM


  • Lifetime Qt Champion

    @Q139
    well you should never have data in the .h file.

    but you can using external;
    in .h
    extern TCP_Socket * TheOnlyInstance;

    and in .cpp
    TCP_Socket * TheOnlyInstance;



  • Thanks , will read materials on circular dependency as have had many problem with this.


  • Lifetime Qt Champion

    @Q139
    well we all have :)

    Well in your case:
    using external.

    then in mainwindow, u include TCP_Socket.H

    then you have the instance to give to the other objects when you create them


  • Lifetime Qt Champion

    @Q139 said:

    there are multiple instances of sockets and each one should have fileCOM

    the same file com?



  • Yes same class but different filenames.
    Foward declaration is good solution and compiles , if it work good , appears to be complex solution for easy problem.

    .h file

    class TCP_Socket;

    class FileCOM : public QObject
    {
    public:
    TCP_Socket * SOCKETptr;
    FileCOM(TCP_Socket * ptr){
    SOCKETptr=ptr;
    }

    .h file

    class TCP_Socket : public QObject
    {
    public:
    FileCOM * FILE;

    .cpp file

    void TCP_Server_File::NewConnection()
    {
    TCP_Socket *soc = new TCP_Socket;
    soc->FILE = new FileCOM(soc); <--- if working then here it should pointer of TCP_Socket class to FileCOM class.
    soc->New_socket (server->nextPendingConnection ());
    mainwin_ptr->AskForNewConnection (soc);
    Client_count++;
    }


  • Lifetime Qt Champion

    it seems fine.
    SOCKETptr should be the object u expect.


Log in to reply