Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:25:59+00:00 2026-06-04T16:25:59+00:00

I have a thread-class Buffer (own made class), and many derived classes such as

  • 0

I have a thread-class Buffer (own made class), and many derived classes such as BufferTypeA, BufferTypeB…

Since I have to synchronize them in a certain order, I’m giving any of them an integer which represents the order to run certain task. I also have to know inside each thread Buffer which one is next to run the task, so I’m passing every BufferType a reference to an integer which all of them must share and I didn’t want to make it Global.

I got lost at any point and I don’t see where.

First I create all the BufferTypes from a class where I also define that shared integer as:

int currentThreadOrder;

And when creating the BufferTypes:

int position = 0;
    if (NULL == bufferA) {
            bufferA = new BufferTypeA(&currentThreadOrder, ++position,
                        waitCondition);
        }
        if (NULL == bufferB) {
            bufferB = new BufferPos(&currentThreadOrder, ++position,
                        waitCondition);
        }
        if (NULL == bufferC) {
            bufferC = new BufferRtk(&currentThreadOrder, ++position,
                        waitCondition);
        }

Then, in BufferTypeA header:

class BufferTypeA: public Buffer {
public:
    BufferTypeA(int currentThreadOrder,
            int threadConnectionOrder = 0,
            QWaitCondition *waitCondition = NULL);
//..
}

And in cpp file:

BufferTypeA::BufferTypeA(int currentThreadOrder, int threadConnectionOrder, QWaitCondition *waitCondition):
        Buffer(currentThreadOrder, threadConnectionOrder, waitCondition) { }

Now I’ll show Buffer header:

    class Buffer: public QThread {
    public:
        Buffer(int &currentThreadOrder,
                int threadConnectionOrder = 0,
                QWaitCondition *waitCondition = NULL);
    //...
protected:
    QWaitCondition *waitCondition;
    int threadConnectionOrder;
    int &currentThreadOrder; // Shared address
    }

And finally the cpp:

Buffer::Buffer(int &currentThreadOrder, int threadConnectionOrder, QWaitCondition *waitCondition) {

    this->threadConnectionOrder = threadConnectionOrder;
    this->waitCondition = waitCondition;
    this->currentThreadOrder = currentThreadOrder;
}

And the error I’m getting is error: uninitialized reference member Buffer::currentThreadOrder.

I’m embarrased to ask, because it’s going to be a simple problem with pointers and addresses, but I can’t see where the problem is, so please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:26:00+00:00Added an answer on June 4, 2026 at 4:26 pm

    When you create a class with a data-member that is a reference, the reference needs to be assigned a value in the constructor initializer list.

    References have to be given a value when they are created, they are not pointers. They have to start with a value and that value cannot be changed (while the contents that is pointed to by that value can be changed).

    Essentially you can think of a reference as an alias for an existing variable. You can’t give a friend a nickname if you don’t have a friend 🙂

    RESPONSE TO COMMENT:

    You don’t “share a reference” between objects. Each object will have its own reference to the same variable. When you “pass by reference” you are telling the compiler that you want the variable in your function to actually be the variable in your outer scope, rather than creating a new variable by value. This means that you only have one variable at one memory location. The reference is just memory in some other place that forwards you to that same memory location.

    Think of this as call forwarding… I can have 15 phone numbers in 15 different countries. I can set them all up to forward calls to my cell in the US. So, people are calling me no matter which number they call.

    Each of your classes just has another reference to forward the “phone calls” or variable reads/writes to that same memory location. So, you’re not sharing a reference between classes, you’re making sure that each class HAS a reference to the same underlying memory location.

    Back to the metaphore, each class won’t have the same phone, but each class’ phone will forward to the same number (variable) none-the-less which lets them all set/get the same value in the end.

    RESPONSE II:

    Here’s a simple example to get your head going, it’s pretty easy to apply to your classes. I didn’t compile it but it should work minus a typo or two possibly.

    class A
    {
        public:
            A(int& shared) : m_shared(shared)
            {
                //No actions needed, initializer list initializes
                //reference above. We'll just increment the variable
                //so you can see it's shared in main.
                m_shared += 7;
            }
    
            void DoSomethingWithIt()
            {
                //Will always reflect value in main no matter which object
                //we are talking about.
                std::cout << m_shared << std::endl;
            }            
    
        private:
    
            //Reference variable, must be initialized in 
            //initializer list of constructor or you'll get the same
            //compiler error again.
            int& m_shared;
    };
    
    int main()
    {
        int my_shared_integer = 0;
    
        //Create two A instances that share my_shared_integer.
        //Both A's will initialize their internal reference to
        //my_shared_integer as they will take it into their
        //constructors "by reference" (see & in constructor
        //signature) and save it in their initializer list.
        A myFirstA(my_shared_integer);
        A mySecondA(my_shared_integer);
    
        //Prints 14 as both A's incremented it by 7 in constructors.
        std::cout << my_shared_integer << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across many ad hoc implementations of thread class in C++, but
I have class A and classes B and C. class B runs one thread
I have the following thread: class LemonadeMainMenuThread extends Thread { private SurfaceHolder mSurfaceHolder; private
I have a custom thread pool class, that creates some threads that each wait
I have a slightly modified version of the thread class copied off the Linux
I have a class class with main thread public class MainThread extends Thread {
I have a worker thread in a class that is owned by a ChildView.
I have a class (NamedPipeManager) which has a thread (PipeThread) that waits for a
I have a DirectoryMonitor class which works on another thread. It has the following
I have a question about the 'Event Dispatch Thread'. I have a Main class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.