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

  • Home
  • SEARCH
  • 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 173799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:24:26+00:00 2026-05-11T13:24:26+00:00

After reading a question on the difference between pointers and references , I decided

  • 0

After reading a question on the difference between pointers and references, I decided that I’d like to use references instead of pointers for my class fields. However it seems that this is not possible, because they cannot be declared uninitialized (right?).

In the particular scenario I’m working on right now, I don’t want to use normal variables (what’s the correct term for them by the way?) because they’re automatically initialized when I declare them.

In my snippet, bar1 is automatically instantiated with the default constructor (which isn’t what I want), &bar2 causes a compiler error because you can’t use uninitialized references (correct?), and *bar3 is happy as larry because pointers can be declared uninitialized (by the way, is it best practice to set this to NULL?).

class Foo { public:     Bar bar1;     Bar &bar2;     Bar *bar3; } 

It looks like I have to use pointers in this scenario, is this true? Also, what’s the best way of using the variable? The -> syntax is a bit cumbersome… Tough luck? What about smart pointers, etc? Is this relevant?

Update 1:

After attempting to implement a reference variable field in my class and initializing it in the constructor, why might I receive the following error?

../src/textures/VTexture.cpp: In constructor ‘vimrid::textures::VTexture::VTexture()’: ../src/textures/VTexture.cpp:19: error: uninitialized reference member ‘vimrid::textures::VTexture::image’ 

Here’s the real code:

// VTexture.h class VTexture { public:     VTexture(vimrid::imaging::ImageMatrix &rImage); private:     vimrid::imaging::ImageMatrix ℑ }  // VTexture.cpp VTexture::VTexture(ImageMatrix &rImage)     : image(rImage) { } 

I’ve also tried doing this in the header, but no luck (I get the same error).

// VTexture.h class VTexture { public:     VTexture(vimrid::imaging::ImageMatrix &rimage) : image(rImage) { } } 

Update 2:

Fred Larson – Yes! There is a default constructor; I neglected it because I thought it wasn’t relevant to the problem (how foolish of me). After removing the default constructor I caused a compiler error because the class is used with a std::vector which requires there to be a default constructor. So it looks like I must use a default constructor, and therefore must use a pointer. Shame… or is it? 🙂

  • 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. 2026-05-11T13:24:26+00:00Added an answer on May 11, 2026 at 1:24 pm

    Answer to Question 1:

    However it seems that this is not possible, because they [references] cannot be declared uninitialized (right?).

    Right.


    Answer to Question 2:

    In my snippet, bar1 is automatically instantiated with the default constructor (which isn’t what I want), &bar2 causes a compiler error because you can’t use uninitialized references (correct?),

    You initialize references of your class in your constructor’s initializer list:

    class Foo { public:     Foo(Bar &rBar) : bar2(rBar), bar3(NULL)     {     }      Bar bar1;     Bar &bar2;     Bar *bar3; } 

    Answer to Question 3:

    In the particular scenario I’m working on right now, I don’t want to use normal variables (what’s the correct term for them by the way?)

    There is no correct name for them, typically you can just say pointers for most discussions (except this one) and everything you need to discuss will also apply to references. You initialize non pointer, non reference members in the same way via the initailizer list.

    class Foo { public:    Foo()  : x(0), y(4)   {   }    int x, y; }; 

    Answer to Question 4:

    pointers can be declared uninitialized (by the way, is it best practice to set this to NULL?).

    They can be declared uninitialized yes. It is better to initialize them to NULL because then you can check if they are valid.

    int *p = NULL; //...  //Later in code if(p) {   //Do something with p } 

    Answer to Question 5:

    It looks like I have to use pointers in this scenario, is this true? Also, what’s the best way of using the variable?

    You can use either pointers or references, but references cannot be re-assigned and references cannot be NULL. A pointer is just like any other variable, like an int, but it holds a memory address. An array is an aliased name for another variable.

    A pointer has its own memory address, whereas an array should be seen as sharing the address of the variable it references.

    With a reference, after it is initialized and declared, you use it just like you would have used the variable it references. There is no special syntax.

    With a pointer, to access the value at the address it holds, you have to dereference the pointer. You do this by putting a * before it.

    int x=0; int *p = &x;//p holds the address of x int &r(x);//r is a reference to x //From this point *p == r == x *p = 3;//change x to 3 r = 4;//change x to 4 //Up until now int y=0; p = &y;//p now holds the address of y instead. 

    Answer to Question 6:

    What about smart pointers, etc? Is this relevant?

    Smart pointers (See boost::shared_ptr) are used so that when you allocate on the heap, you do not need to manually free your memory. None of the examples I gave above allocated on the heap. Here is an example where the use of smart pointers would have helped.

    void createANewFooAndCallOneOfItsMethods(Bar &bar) {     Foo *p = new Foo(bar);       p->f();     //The memory for p is never freed here, but if you would have used a smart pointer then it would have been freed here.   } 

    Answer to Question 7:

    Update 1:

    After attempting to implement a reference variable field in my class and initializing it in the constructor, why might I receive the following error?

    The problem is that you didn’t specify an initializer list. See my answer to question 2 above. Everything after the colon :

    class VTexture { public:     VTexture(vimrid::imaging::ImageMatrix &rImage)       : image(rImage)     {      } private:     vimrid::imaging::ImageMatrix ℑ } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For deletes, you can set up a DML trigger (see… May 11, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer You can provide this in jboss.xml http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/jboss_deployment_descriptor.html jndi-name element is… May 11, 2026 at 10:09 pm
  • Editorial Team
    Editorial Team added an answer Check your sources. A lot of people want SVG "dead".… May 11, 2026 at 10:09 pm

Related Questions

I'm reading this C++ open source code and I came to a constructor but
Im looking for an algorithm to be used in a racing game Im making.
I have a script that needs to extract data temporarily to do extra operations
I have to sort a number of integers, which can have values between 30.000.000

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.