operator-overloading

Overload vector subscript operator to take a char * or string

烂漫一生 提交于 2020-01-05 05:28:12
问题 I am trying to overload the subscript operator -i know it as the element access operator- to take a char * or a std::string. I have a struct struct foo { int Age; const char *Name; }; and a std::vector that will be holding multiple instances of this struct. std::vector<foo>bar; my goal is to be able to access each foo in bar by calling them by their name. std::cout<<"Simon's age is: "<<bar["simon"]; I've just been searching google for a long while trying to find an example or something to go

Utility functions for class constructors, destructor, and operator overloading

谁都会走 提交于 2020-01-05 05:25:11
问题 A while ago, I found in a website some code examples of utility functions that are used when creating , destructing objects, or even when overloading some of their operators . More precisely, the following member functions are mainly used: init, copy, set, and destroy. The init member function is used to initialize all the private members. It's mostly called inside the constructors , e.g. the default or parameter constructor . The copy member function is used to do a deep copy of an object

Fortran function to overload multiplication between derived types with allocatable components

╄→尐↘猪︶ㄣ 提交于 2020-01-05 03:46:07
问题 Foreword In order to store banded matrices whose full counterparts can have both rows and columns indexed from indices other than 1 , I defined a derived data type as TYPE CDS REAL, DIMENSION(:,:), ALLOCATABLE :: matrix INTEGER, DIMENSION(2) :: lb, ub INTEGER :: ld, ud END TYPE CDS where CDS stands for compressed diagonal storage. Given the declaration TYPE(CDS) :: A , The rank-2 component matrix is supposed to contain, as columns, the diagonals of the actual full matrix (like here, except

user-defined conversion operators precedence, compiles in g++ but not clang++

五迷三道 提交于 2020-01-04 09:15:14
问题 I have the following code, which is a wrapper for POD into a template class Foo<T> , where T is the wrapped type (can be int , double etc). I define a templated conversion operator, and also the friend addition operator+ , see code below. In the last line of main() , I define Foo<int> a = 10 then compute cout << 2.1 + a << endl; // outputs 12 What is the rule here? It looks like the expression is being translated as operator+(2.1, a) which then becomes operator+(Foo<int>(2.1), a) . Why doesn

Create my own operator in c++

自作多情 提交于 2020-01-04 06:35:38
问题 I know it is possible to overload operators that already exist in c++ to define desired behavior, but is it possible to create your own operator? Just for example, making an operator # that returns the size of containers: template<typename T> size_t operator#(const T& obj) { return obj.size(); } vector<int> v(1024); cout << #v; // prints 1024 回答1: No. You need to stick with the operators the parser already knows how to handle. Overloading can extend the meaning of expressions, but the syntax

Create my own operator in c++

前提是你 提交于 2020-01-04 06:35:09
问题 I know it is possible to overload operators that already exist in c++ to define desired behavior, but is it possible to create your own operator? Just for example, making an operator # that returns the size of containers: template<typename T> size_t operator#(const T& obj) { return obj.size(); } vector<int> v(1024); cout << #v; // prints 1024 回答1: No. You need to stick with the operators the parser already knows how to handle. Overloading can extend the meaning of expressions, but the syntax

Why is implicit conversion between anonymous access objects disallowed in Ada?

怎甘沉沦 提交于 2020-01-04 05:15:12
问题 I am working my way through Barnes' book 'Programming in Ada 2012'. This is a code sample implementing a stack from section 12.5. src/stacks.adb: (the main relevant file) package body Stacks is procedure Push(S: in out Stack; X: in Integer) is begin S := new Cell'(S,X); end Push; procedure Pop(S: in out Stack; X: in out Integer) is begin X := S.Value; S := Stack(S.Next); end Pop; function "="(S, T: Stack) return Boolean is SS: access Cell := S; TT: access Cell := T; begin while SS /= null and

operator-> repeats until it returns a value of non-class type

蓝咒 提交于 2020-01-03 16:55:12
问题 According to 13.3.1.2/8, or better footnote-129 (emphasis mine): [...] The process repeats until an operator-> function returns a value of non-class type . I thought I knew how operator-> works (let me say, its recursive way based on the return type), but I see that I'm completely unaware about how it actually works (I mean, its return type ). When I found it, I wondered if one can really define and use something like a double operator->() for a generic struct S , for I've never used such an

C++ Defining the << operator of an inner class

一世执手 提交于 2020-01-03 12:59:49
问题 Working on a project I did not initiate, I want to add an << operator to a class. Problem: the class is a private inner class of an other class, the latter being in a namespace . And I cannot make it. The problem can be simplified this way: #include <iostream> #include <map> namespace A { class B { private: typedef std::map<int, int> C; C a; friend std::ostream& operator<<(std::ostream& os, const C &c) { for (C::const_iterator p = c.begin(); p != c.end(); ++p) os << (p->first) << "->" << (p-

Chaining implicit operators in generic c# classes

喜夏-厌秋 提交于 2020-01-03 10:31:07
问题 For the following generic c# class, I'd like to convert T to K: public abstract class ValueType<T,K> : IValueType<T> where K : ValueType<T,K>,new() { public abstract T Value { get; set; } public static implicit operator ValueType<T,K>(T val) { K k = new K(); k.Value = val; return k; } } If I were to implement a direct operator implicit operator K(T val) it would result in a compile-time error (CS0556). I thought I could try chaining implicit operators: public static implicit operator K