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 8230753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:14:20+00:00 2026-06-07T17:14:20+00:00

Consider following class of custom array template <typename element, unsigned int size> class array

  • 0

Consider following class of custom array

template <typename element, unsigned int size>
class array
{
private:
    element (&data)[size];
public:
    array(element (&reference)[size]):data(reference){}
    array():data(new element [size]){} // This is where my problem arises
};

Obviously, in initialization list of constructor evaluates the rvalue expression (the new-expression) to rvalue of pointer instead of reference type. So in MSVC (Microsoft Visual Studio 2012 RC) prompts the error about type conversion from element * to element (&)[size].

However, I think that if I can perhaps convert the rvalue from pointer to reference, it should compile. Is there any chance I can make it there in initialization list of constructor? Or perhaps it just all conflicts with the principle of C++ language? I’ve investiaged into reinterpret_cast, but the only rule I find relevant is

“An lvalue expression of type T1 can be converted to reference to
another type T2. The result is an lvalue or xvalue referring to the
same object as the original lvalue, but with a different type.” – cppreference.com

which clearly states that the conversion applies to lvalue expression.

OK, I know what you might think now – why do you need such class of custom array and why don’t you just composite pointer instead of the problemtic reference? Well…you always need different approaches to choose before knowning the best don’t you?

  • 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-07T17:14:21+00:00Added an answer on June 7, 2026 at 5:14 pm

    What’s this about C++11 and rvalue references? To get a lvalue expression to the value pointed to by a pointer, just use the unary * operator. It’s been part of the language since the beginning. All you have to do here is make sure you get a pointer to an array, rather than a pointer to the first element of the array.

    template <typename element, unsigned int size>
    class array
    {
    private:
        element (&data)[size];
    public:
        array() : data(*new element[1][size]) { }
        array(element (&reference)[size]) : data(reference) { }
    };
    

    As mentioned by Mat in his comment, the parameterless constructor here is a very bad idea, because the memory will not be freed. Hopefully, having a working code sample will allow you to experiment a bit and come to the same conclusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Consider the following: class A { private: A() {} public: A(int x = 0)
Consider the following : class A { public: int xx; A(const A& other) {
Let's consider following class template of custom array in Microsoft Visual C++ (Microsoft Visual
Consider the following: class MyClass { public: int operator ()(int a, int b); };
Consider the following class public class Class1 { public int A { get; set;
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider the following class: public class Score { private static readonly Guid _relationId =

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.