assigning value to an array

novice1618

This is just the outline of the code I am trying.please help me !

void surfaceintensity(int xpos,int ypos ,int zpos)
{
    x[1]=xpos;
    x[2]=ypos;
    x[3]=zpos;
}

Suppose I have an object t1 and I have sent values to the function surface intensity as:

t1.surfaceintensity(10,20,30)

If i do it above mentioned way then,will the values of

x[1]=10;
x[2]=20;
x[3]=30;

If not how can I assign these values to the array x[]?

Alexander Tobias Bockstaller

The way I understand your question, you have a class (let's call it MyClass) which has a member function surfaceintensity(). This member function assigns some values to the elements of an array x which is also a member of your class.

You're unsure if assigning values to that array from inside the member function will actually change the array of the instance its called upon. If that is the case, then look at the following example (just copy/paste it, it should compile):

#include <iostream>

class MyClass
{
public:
    MyClass()
    {
        x[0] = 0;
        x[1] = 0;
        x[2] = 0;
    }

    void surfaceintensity(int xpos,int ypos ,int zpos)
    {
        x[0]=xpos;
        x[1]=ypos;
        x[2]=zpos;
    }

    void print()
    {
        std::cout << x[0] << "/" << x[1] << "/" << x[2] << std::endl;
    }

private:
    int x[3];
};

int main()
{
    MyClass t1;
    t1.print();
    t1.surfaceintensity(10,20,30);
    t1.print();
    return 0;
}

This will print

0/0/0
10/20/30

This demonstrates that the answer to your question is: yes, assigning values to member variables does change the internal state of the object.

I hope this was what you we're asking. If not, please edit your question and clarify.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assigning a random value to an array

Assigning value to the array of function pointers

Assigning Array Value to variable PHP

Assigning value to array at certain index

Assigning a priority value to objects in an array

Assigning properties value to an array in powershell

Assigning value to array but value doesn't change?

Assigning cell value to an array based on a condition

For loop assigning only the last value of an array to eventlisteners

command not found assigning value to array in bash

Assigning default value to variable in destructing nested array

Assigning an array value in zsh for gsettings keybindings automation

Assigning a value which comes from a loop to an array

Assigning array elements as object properties, with 'counter' value

assigning new value to an object in object array in jquery

Assigning a value to numpy array with a boolean mask: how to?

Swift Decodable issue - Trouble assigning value to an array

Assigning value by index in xarray assignes whole array

Assigning value to element in array changes other variable

Assigning value to symbol in particular index in symbolic array

Assigning value in array in foreach loop error

Assigning value to an array of other class by creating an object

Assigning value to array element in assembly (GAS syntax)

JavaScript - Assigning Array Index to Return Value not working

Temp variable not assigning right value from array

C - Segmentation Fault Assigning value to Array of Struct

Assigning 2 dimensional array value to a variable

Problem assigning integer a value from an array index

Assigning a minimum value (array.Min()) to an element in an array in a custom method