structure operator

2019. 5. 9. 08:43 from Language/C++

operator concept


기존에 있던 operator 에 overloading을 하여 새로운 연산 방법을 추가하는 것. 함수 overloading을 연산자에 적용했다고 생각하면 된다.




operator structure


함수랑 똑같다. <operator> 는 그대로 써주어야 하고 (input operator) 에는 overloading 하고 싶은 연산자를 집어넣으면 된다. 그리고 input parameter 는 "input operator"가 연산할 내용.


(return type) <operator> (input operator) (input parameter)


"input operator" 에 대해서 input 받은 "input parameter"를 <operator> 에서 연산해서 (return type)으로 반환한다.



Example


* 참고로 "constructor _rgb (uint32_t rgb)"만 만들어 놓으면 "_rgb tmp;" 에서 오류가 발생하므로 "constructor _rgb ( ) { }" 형태도 만들어 놓아야 한다.


inline _rgb operator+(_rgb rgb): input 으로 들어온 rgb 의 attribute 들을 struct member attribute 에 더해서 새로운 _rgb 출력.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
struct _rgb
{
    uint8_t r;
    uint8_t g;
    uint8_t b;
 
    _rgb ()
    { } 
 
    _rgb (uint32_t rgb)
    {
        rgb = *reinterpret_cast<int*>(&rgb);
        r = (rgb >> 16& 0x0000FF;
        g = (rgb >> 8)  & 0x0000FF;
        b = (rgb)       & 0x0000FF;
    }
 
    inline _rgb operator+(_rgb rgb)
    {
        _rgb tmp;
        tmp.r = rgb.r + r;
        tmp.g = rgb.g + g;
        tmp.b = rgb.b + b;
 
        return tmp;
    }
 
    inline _rgb operator*(float fScaleFactor)
    {
        _rgb tmp;
        tmp.r = (float)r * fScaleFactor;
        tmp.g = (float)g * fScaleFactor;
        tmp.b = (float)b * fScaleFactor;
 
        return tmp;
    }
};
typedef struct _rgb RGB; 
cs




Usage


 fromColor*(1 - fScaleFactor) +: _rgb structure 가 input 으로 toColor*fScaleFactor; 받고 있다. 그리고 출력으로 _rgb tmp; 에서 새롭게 만든 _rgb data type 반환해서 RGB color 로 입력

1
2
3
4
5
6
7
8
9
10
for (size_t i = 0; i < pCloudColored->points.size(); i++)
    {
        float fScaleFactor = (globalDistance.fDistances[i] - globalDistance.fMin_m)/(globalDistance.fMax_m - globalDistance.fMin_m);
        
        RGB color = fromColor*(1 - fScaleFactor) + toColor*fScaleFactor;
 
        uint32_t rgb = (static_cast<uint32_t>(color.r) << 16 |
                static_cast<uint32_t>(color.g) << 8 | static_cast<uint32_t>(color.b));
        pCloudColored->points[i].rgb = *reinterpret_cast<float*>(&rgb); 
    }
cs


Posted by 나무길 :

Enum.Parse

2018. 10. 27. 05:27 from Language/C#

Enum.parse


Enum.parse를 이용해서 enum 맴버에 해당하는 string으로 switch case 문 사용하기


1
2
3
4
5
6
7
8
9
10
11
12
string Animal = "Dog";
 
switch (Enum.Parse(typeof(TestEnum), Animal))
{
    case TestEnum.Dog:
        Console.WriteLine("Dog");
        break;
    case TestEnum.Cat:
        Console.WriteLine("Cat");
        break;
    defaultbreak;
}
cs


Posted by 나무길 :

public bool testBool { get; set; }

아무런 초기화 없이 바로 testBool 을 호출하면 무슨 value를 반환할까?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Program
{
    static void Main(string[] args)
    {
        testing MyTesting = new testing();
 
        if (MyTesting.testBool == falseConsole.WriteLine("bool is false");
        if (MyTesting.testBool == trueConsole.WriteLine("bool is true");
        if (MyTesting.testBool == nullConsole.WriteLine("bool is null");
    }
}
 
class testing
{
    public bool testBool { get; set; }
}
 
// output :
// bool is false
cs


'Language > C#' 카테고리의 다른 글

Enum.Parse  (0) 2018.10.27
Upcasting and Downcasting  (0) 2018.10.24
public, private, protected, internal keyword  (0) 2018.10.24
async await 사용시 조심해야 할 점  (0) 2018.10.14
Object Oriented Robot Programming  (0) 2018.10.14
Posted by 나무길 :