I have a class called "Piece" and an object "Object 1" of this class. I want to create another object "Object 2" which is an exact copy (i.e. it's members have the same values) of "Object 1". How do I do this?
The members are handles so I'm guessing I need to dereference them and then set each of Object 2's members equal to those of Object 1. What is the syntax for this in C++/CLI?
Thanks in advance for your help.
public ref class Piece
{
Type1 ^ member1;
Type2 ^ member2;
Type3 ^ member3;
public:
Piece(Piece ^ other)
: member1(other->member1), member2(other->member2), member3(other->member3)
{
//other stuff
}
//... Other Functions,
// constrtuctors,
// destructors etc.
}
Related
According to Kotlin documentation:
Members of the companion object can be called by using simply the
class name as the qualifier.
Why does it not seem to work here?
class Foo {
companion object {
enum class Type { A, B, C }
}
}
class Bar {
val typeA = Foo.Companion.Type.A // works
val typeB = Foo.Type.B // error: "Unresolved reference: Type"
}
Comparing the two qualified type names, Foo.Type.A and Foo.Companion.Type.A , the former would rather mean a type declared directly inside the Foo's scope.
The latter form, therefore, is used to disambiguate types declared inside a type from ones declared inside its nested types and object declarations (including the companion object).
class Foo {
class Bar // Foo.Bar
companion object {
class Bar // Foo.Companion.Bar
}
object Baz {
class Bar // Foo.Baz.Bar
}
}
As Pawel noted, nested types and object declarations are not members and have different resolution rules than those of functions and properties.
class Test {
int a = 100;
System.out.println(a);
}
class Demo {
public static void main(String args[]) {
Test t = new Test();
}
}
I'm new to programming. I found this code when I'm practicing. I don't understand why I'm getting this error.
Here is the error I'm getting.
Demo.java:3: error: <identifier> expected
System.out.println(a);
^
Demo.java:3: error: <identifier> expected
System.out.println(a);
^
2 errors
Compilation failed.
Can you guys explain why I'm getting this error?
You can't call a method directly from the java class body.
Create a constructor in your Test class, and put the print in it :
class Test {
int a = 100;
public Test() {
System.out.println(a);
}
}
Note that if for some reason you really want a statement to be executed when the class is loaded without using a constructor, you can define a static block, here an example :
class Test {
static int a = 100;
static {
System.out.println(a);
}
}
However, this is just for reference and really not needed in your case.
From Declaring Classes in the Java tutorial:
In general, class declarations can include these components, in order:
Modifiers such as public, private, and a number of others that you will encounter later.
The class name, with the initial letter capitalized by convention.
The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
The class body, surrounded by braces, {}.
You can't make any function calls outside of a method declaration.
I have noticed a situation where there is a class (say: ClassA) with variable declarations and various methods. And in another class (say: Class B), there is a method(MethodofClassB()) with the return type of the method as ClassA.
so it is like:
Class A
{
variable i,j;
public int MethodA()
{
//some operation
}
}
Class B
{
variable x,y;
public static A MethodB()
{
//some operation
return obj;
}
}
1) I understand that MethodB() return an object of ClassA. Waty would be the use(the intention) of returning the object of ClassA
2) What is the reason for defining MethodB() as Public static. what would happen if static was not used for MethodB()
3)What would the returned objct look like. I mean if my method returned an integer, it would return some numerical value say '123' . If a method returns an object of a class, what would be in the returrned value.
please help me understand this with a small example
1) I understand that MethodB() return an object of ClassA. Waty would be the use(the intention) of returning the object of ClassA
Depends on what the method does, which isn't illustrated in this example. If the result of the operation is an instance of A then it stands to reason that it would return an instance of A, whatever A is.
For example, if A is a Car and B is a CarFactory then the method is likely producing a new Car. So it would return a Car that's been produced.
2) What is the reason for defining MethodB() as Public static. what would happen if static was not used for MethodB()
public allows it to be accessed by other objects. static means it's not associated with a particular instance of B. Both are subjective based, again, on the purpose of the method (which isn't defined in the example). Being static, it can be called as such:
var newInstance = B.MethodB();
If it wasn't static then an instance of B would be required:
var objectB = new B();
var newInstance = objectB.MethodB();
There are more and more implications here, including things like memory/resource usage and thread safety. All stemming from the purpose and business logic meaning of what B is and what MethodB does.
3)What would the returned objct look like. I mean if my method returned an integer, it would return some numerical value say '123' . If a method returns an object of a class, what would be in the returrned value.
It would be an instance of A. Similar to creating an instance here:
var objectA = new A();
This method also creates (or in some way gets) an instance:
var objectA = B.MethodB();
Without knowing more about what A is, what its constructor does, and what MethodB does, these two operations are otherwise the same.
First, your code is incorrect. There is no "ClassA" class. The class name is A, so the return type should be A not ClassA.
Second, the standard Java coding standards say to start methods and variables with lower case letters. So, your example should have been:
Class A
{
A anA;
B aB;
public int methodA()
{
//some operation
}
}
Class B
{
SomeType x, y;
public static A methodB()
{
//some operation
return obj;
}
}
David's answer shortly before mine is technically correct on points 1 and 2, although he also uses your mistake of calling the A type ClassA. His code for his answer to point 3, though, is incorrect and misleading. I would change his wording to this:
`3)What would the returned objct look like. I mean if my method returned an
integer, it would return some numerical value say '123' . If a method returns
an object of a class, what would be in the returrned value`.
It would be an instance of class A. Similar to creating an instance here:
A objectA = new A();
This method also creates (or in some way gets) an instance:
A objectA = B.methodB();
Without knowing more about what class A is, what its constructor does, and what methodB does, these two operations are otherwise the same.
This question already has answers here:
Initializing PHP class property declarations with simple expressions yields syntax error
(3 answers)
Closed 9 years ago.
Is there a way in PHP the class definition to have a property set to a STD object with set properties?
I was thinking you can type cast but its not allowed for properties in the class definition.
//example.
class Foo{
private static $Obj = (object) ['bizz', 'bazz'];
}
which will trigger this error:
Parse error: syntax error, unexpected '(object)' (object) (T_OBJECT_CAST)
You cannot assign non-constant values to class properties during declaration. From http://php.net/language.oop5.properties
...initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
You will need to use a class method to initialise the property, eg
class Foo {
private static $Obj;
private static getObj() {
if (null === self::$Obj) {
self::$Obj = (object) ['bizz', 'bazz'];
}
return self::$Obj;
}
RS232MsgGetEventDescriptions.h:
#define DECLARE_RS232_NEWMSG(ClassID)\
enum \
{ \
ID = ClassID \
}; \
#interface RS232MsgGetEventDescriptions : RS232Msg
{
}
#end
RS232MsgGetEventDescriptions.m
#implementation RS232MsgGetEventDescriptions
DECLARE_RS232_NEWMSG(RM_GET_EVENT_DESCRIPTIONS);
#end
EventLogs.m
-(void)event
{
service = [CServiceAppDlg alloc];
if ([service:(REMOTE_MESSAGE_ID)RS232MsgGetEventDescriptions.ID withEvent:pEvent])
{
NSLog(#"Get Event descriptions!!");
}
}
I'm getting an error like "Accessing Unknown 'ID' class method"
I should not modify the definition here.How to pass the ID.I am going to call different descriptions ID in the same way so is this declaration of ID.
The reason why you are getting a Accessing Unknown 'ID' class method error message is because you have not declared a method called ID in your class RS232MsgGetEventDescriptions.
When you say RS232MsgGetEventDescriptions.ID in your code, you are calling the property ID of object RS232MsgGetEventDescriptions, which is equivalent to [RS232MsgGetEventDescriptions ID]. However, RS232MsgGetEventDescriptions is not an object, but a class and you don't have a class method called + (REMOTE_MESSAGE_ID)ID in your class specification (you don't have it declared on the interface or implemented on the class implementation).
I would also like to point out that it is bad practice to use the dot notation for something other than a property. Since classes cannot have #properties (these are for objects) you should call this method using standard Objective-C messaging notation [RS232MsgGetEventDescriptions ID].
Xcode will still allow you to write object.methodName to call methods with no parameters, and object.methodName = value for methods that take 1 parameter. Because they are interpreted as follows:
object.methodName; // Becomes [object methodName]
object.methodName = value; // Becomes [object setMethodName:value]