0

Hey, I have a question. I was writing a custom Array type class. In my template declaration I use the type name as DataType. I was making an attempt at creating an insert method, which takes a DataType argument and the index to insert at. I originally had this code:

void insert(DataType p_item, int p_index)
    {
        int index;
        for(index = m_size-1;index>p_index;index--)
        {
            m_array[index] = m_array[index-1];
        }
        m_array[p_index] = p_item;
    }

Where m_array is the created array, and m_size is it's size. But it was unable to insert into an array which was full, because of how I tried to solve the problem.
So I ended up simplifying it completely:

void insert(DataType p_item, int p_index)
    {
        DataType m_item;
        m_array[p_index] = m_item;
        m_array[p_index] = p_item;
    }

My question is, what value will the m_item default too? Will this cause problems in the future? When I printed out the array to test it(before I reassigned p_item to the array), it printed an empty space.

Edited by Akill10: n/a

3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by Akill10
Featured Replies
  • 1

    Then my answer still stands, m_item will be initialized based on its type ;). If DataType is an int, m_item will be initialized as if it were written 'int m_item;'. So if you call insert, where DataType is deduced as an integer, the compiler will emit code that treats m_item … Read More

0

You mean what value m_item will hold? That depends on the type of DataType.
It's default constructor will be called, without any parameters. If DataType is an int, it will behave just like 'int m_item;' would. Not sure if this is exactly your question though (just had an exam from 18:30 -> 22:00... I might not think clearly any more ;)).

I'm not actually sure what you're trying to do with your simplified version.

m_array[p_index] = m_item;
m_array[p_index] = p_item;

// Same as:
m_array[p_index] = p_item; // ??
0

Well,
DataType will get the type you pass it, no? So if it's a char, it may take just a numeric value and then convert it to ASCII.
If it's a int, it'll be a zero. The same for double, float and bool
Maybe you SHOULD initialize it to NULL.
The problem you may get it if DataType is a string or a char[].
No debug mistakes, Yes unhandled exception, null not considered blablabla.

0

And the lamb is right... I mean, don't you overwrite the same array[index] when you give it

m_array[p_index] = m_item;
m_array[p_index] = p_item;

?

0

m_item is never initialised, I am asking what the value will be?, not the type. Initialising the NULL is not possible, it's too ambiguous according to the compiler. Probably because since it doesn't have a type yet, it wont know if that type will support NULL?

And yes, That is the same thing, I was just trying to make the code so there is no chance for overwriting the wrong thing, obviously not needed now after that was pointed out lol.

0

Well,
really it depends of what type it gets: it maybe a random number as a null point or a zero... it may cause problems, if that's your question.

1

Then my answer still stands, m_item will be initialized based on its type ;).

If DataType is an int, m_item will be initialized as if it were written 'int m_item;'.

So if you call insert, where DataType is deduced as an integer, the compiler will emit code that treats m_item exactly as if it were an integer.

Then your question boils down to "What is an uninitalized integer initialized to"

int m_item;

Same as if you could call insert where DataType is of class A, the compiler will emit code that treats m_item as of type A, and your question is "What is an uninitialized variable of type A initialized to?"

A m_item;
Comments
Thanks
0

Ye sorry, So what I had wouldn't cause any major problems is what I really wanted to know, it would just default to the default of the type. I only had the problem because I thought in c++, variables will not always initialise to the default and can cause some errors. Like, they will end up with some gobble-dee-gook. Thanks for your help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.