How to initialize class with pointer to class
-
wrote on 6 Nov 2015, 12:59 last edited by Q139 11 Jun 2015, 13:03
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 '' tokenvoid set_file_location(); void read_file_to_buffer(); void process_buffer(); void write_file(QByteArray data); std::vector <QByteArray> read_buffer;
};
´´´ -
Lifetime Qt Championwrote on 6 Nov 2015, 13:06 last edited by mrjj 11 Jun 2015, 13:07
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 ? -
wrote on 6 Nov 2015, 13:18 last edited by Q139 11 Jun 2015, 13:19
Compiler returns this error msg : error: expected ')' before ' * ' token
-
-
wrote on 6 Nov 2015, 13:43 last edited by Q139 11 Jun 2015, 13:47
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? -
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?This post is deleted! -
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?@Q139
it is normal to include .h many times.Are you using guards ?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#endif -
wrote on 6 Nov 2015, 13:58 last edited by Q139 11 Jun 2015, 14:00
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
-
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 Championwrote on 6 Nov 2015, 14:00 last edited by mrjj 11 Jun 2015, 14:02Ah, that is called circular include and often solved by using type forwards.
https://en.wikipedia.org/wiki/Circular_dependencylike
in your case
class TCP_Socket; //forward declarationclass FileCOM : public QObject
{
TCP_Socket *mykeptpointer;
....and then DONT include its .h file
-
wrote on 6 Nov 2015, 14:03 last edited by Q139 11 Jun 2015, 14:04
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 -
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 fileCOMLifetime Qt Championwrote on 6 Nov 2015, 14:05 last edited by mrjj 11 Jun 2015, 14:05@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; -
wrote on 6 Nov 2015, 14:06 last edited by
Thanks , will read materials on circular dependency as have had many problem with this.
-
@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
-
@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
@Q139 said:
there are multiple instances of sockets and each one should have fileCOM
the same file com?
-
wrote on 6 Nov 2015, 14:31 last edited by Q139 11 Jun 2015, 14:37
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++;
} -
it seems fine.
SOCKETptr should be the object u expect.
6/16