Initialising values inside structure - structure

When I compiled the below code in gcc I get 1408.How does it work.Can we initialise values inside structure?
#include<iostream>
using namespace std;
struct Test {
int i=1408;
};
int main()
{
Test l;
int p=1508;
l.i==p;
cout<<l.i;
}

l.i==p; does nothing. == tests if two things are equal to each other. That's not an assignment.
You must have meant
l.i=p;

It is best to declare structure variable in struct body and intialise values using objects created using structure identifiers in your code identifier is Test.
Initialisation can be in the form of
Test t1 ={ 1408 };

Related

Testing simple function with Gmock - test fails(should pass?)

I just started using Gtest/Gmocks and I'm struggling with an example. I have a simple class which has a member a function that returns a value(say 3). I'm trying to mock this test and check if the returned result is 3. For simplicity I wrote everything in a single file:
// Testing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "gmock\gmock.h"
#include "gtest\gtest.h"
using ::testing::AtLeast;
using namespace std;
class MyClass{
public:
virtual int retValue() { return 3; }
virtual ~MyClass(){}
};
class FakeMyClass : public MyClass
{
public:
MOCK_METHOD0( retValue, int() );
};
TEST(TestForMyClass, TestRetVal)
{
FakeMyClass obj3;
EXPECT_EQ(obj3.retValue(), 3);
}
int _tmain(int argc, _TCHAR* argv[])
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
However my test fails and it says that expected result is 3 and my actual result is 0.
I've watched a couple of examples and I think I did everything as shown in there still the result is not what I'm expecting. Please help me see where I'm wrong and how can I make that test to pass. Thank you.
The simple answer to your question is:
You have an object of your mock FakeMyClass. This object will never return the value of the base class, if you override this method!!
If you want to test a simple method of a class, you do not need a mock. Just test your class:
class MyClass{
public:
virtual int retValue() { return 3; }
virtual ~MyClass(){}
};
TEST(TestForMyClass, TestRetVal)
{
MyClass obj3;
EXPECT_EQ(obj3.retValue(), 3);
}
Little bit more to mocking:
A principle of testing is to test in isolation. So, when your class is in a relation to another object, you have to mock this object. I suggest to take a look on an example
(e.g. klick).
In this example the Painter is the system under test. The Paintercommunicates with the Turtle, which is mocked.

Why are all of the classes in Rakudo's src/core/Int.pm declared with my?

Looking at the source for Int, I see that all of the classes are declared with my, which I would have thought would make them private and not available outside that file. But, they obviously are. Why do they need to be declared that way?
my class Rat { ... }
my class X::Numeric::DivideByZero { ... }
my class X::NYI::BigInt { ... }
my class Int { ... }
my subset UInt of Int where {not .defined or $_ >= 0};
my class Int does Real { # declared in BOOTSTRAP
I figure that BOOTSTRAP comment has something to do with it. In the Perl6/Metamodel/BOOTSTRAP.nqp there are lines like:
my stub Int metaclass Perl6::Metamodel::ClassHOW { ... };
The files in Rakudo's src/core/ directory are not compiled as separate modules with their own private file-level scope, but concatenated into a single file such as gen/moar/CORE.setting during the build proceess.
Sematically, this 'setting' (known as 'prelude' in other languges) forms an outer lexical scope implicitly surrounding your program.
The design is described in S02: Pseudo-packages, and parts of that section have made it into the official documentation.

I am about to use dlopen() to open shared object. Do I need to include corresponding headers if shared object?

I have to use dlopen() and access functions from shared object in my code. Do I need to include headers of corresponding functions of shared object ?
Because of the way dlopen() and dlsym() operate, I don't see how that would accomplish anything. Very roughly speaking, dlopen() copies the library binary into your program space and adds the addresses of its exported symbols (i.e. global functions & variables) to your program's symbol table.
Because the library was not linked to your program at compile-time, there's no way your code could possibly know the instruction addresses of these new functions tacked on at run-time. The only way to access a run-time dynamically linked symbol is via a pointer obtained from dlsym().
You have to create a function pointer for each and every library definition that you want to use. If you want to call them like regular functions, in C-language you can manually typedef type definitions for the function pointers, specifying their parameters and return values, then you can call the pointers just like regular functions. But note that you have to define all of these manually. Including the library header doesn't help.
In C++ I think there are issues with storing dlsym() output in a typedef'd pointer due to stricter standards, but this should work in C:
addlib.c (libaddlib.dylib):
int add(int x, int y) {
return x+y;
}
myprogram.c:
#include <stdio.h>
#include <dlfcn.h>
typedef int (*add_t)(int, int);
int main() {
void *lib_handle;
add_t add; // call this anything you want...it's a pointer, it doesn't care
lib_handle = dlopen("libaddlib.dylib", RTLD_NOW);
if (lib_handle == NULL) {
// error handling
}
add = (add_t)dlsym(lib_handle, "add");
if (add == NULL) {
// error handling
}
printf("Sum is %d\n", add(17, 23));
dlclose(lib_handle); // remove library from address space
return 0;
}
(Update: I compiled the dylib and myprogram...it works as expected.)

Having trouble accessing variable

I want to find the address of one of the structure's data members, but I'm having trouble accessing its variables. Is there of a solution that donesn't require me to change the struct in any way?
h file
class C
{
private:
int x;
char b;
};
cpp file.
char *p2 = new char[128];
memset(p2,'aa',128);
Test_C *r2 = new(p2) Test_C[3];
//inside for loop
printf("Address: 0x%x, Value of b: %x \n",&r2[i]->b, r[i].r=0x50);
I'm getting the error at &r2[i]->b;
Also some code review would be nice :) I'm planing on outputting values of the C struct with padding
It seems you have posted a C++ class and not a C struct.
From here:
private members of a class are accessible only from within other
members of the same class or from their friends.
So, to answer your question, you cannot access those private members from outside the class without modifying the class itself (to include public accessors, for instance).

Working with structures in C++/CLI

I have following code
ref class A
{
typedef ref struct All
{
std::string x;
}All_t;
};
in my program I am using it in following manner
A::All_t t;
t.X = "H";
This declaration throwing error as
error C4368: cannot define 'x' as a member of managed 'A::All': mixed types are not supported
I understand that I am declaring native varible inside managed code which is not allowed but now I would like to know changes I will need to make to make my structure suitable to managed project.
Thanks.
I'm assuming you originally had std::string x; not std::string *x (since using the pointer to string does not generate that error). You are not allowed to directly embed a native type in a managed type, but you are allowed to indirectly have one (via a pointer) See:
http://msdn.microsoft.com/en-us/library/xhfb39es(v=vs.80).aspx
After I fixed the compiler errors in your sample, it builds without error:
#include "stdafx.h"
#include <string>
using namespace System;
ref class A
{
public:
typedef ref struct All
{
std::string * x;
}All_t;
};
int main(array<System::String ^> ^args)
{
A::All_t t;
t.x = new std::string("H");
return 0;
}